![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi, If I have a simple 'user' table containing some basic information. Something like "fname, lname, displayname, age, city, country, state" and so forth. And, I would like the visitors to be able to search by fname, lname, displayname, age, city, country _or_ state so my select would be "SELECT * FROM table_user WHERE fname='xyz'" Must I create a separate index for each "fname, lname, displayname, age, city, country, state, ..."? Or would a single select be enough? |
|
Would I need to do something different if my select was using a like, ("SELECT * FROM table_user WHERE fname LIKE '%xyz%'")? |
|
Or do you think that the design itself should be different altogether? Many thanks Simon |
#3
| |||
| |||
|
|
On 10/19/2011 5:30 AM, Simon wrote: RDBMS's can only use an index for a single column search if it's the first column in the index. For instance, if you had an index on "lname, fname", the RDBMS can use it for a search on lname, but not for one on fname. |
#4
| |||
| |||
|
|
On 2011/10/19 12:33 PM, Jerry Stuckle wrote: On 10/19/2011 5:30 AM, Simon wrote: RDBMS's can only use an index for a single column search if it's the first column in the index. For instance, if you had an index on "lname, fname", the RDBMS can use it for a search on lname, but not for one on fname. Thanks for the reply and explanation. It would then make sense to create an index for each and every possible select, (and also to _not_ use LIKE '%...%'). In my case, that would mean 13 new indexes. Do you think that the design is flawed in the first place? Should that information be in a different table structure? |
#5
| |||
| |||
|
|
On Wed, 19 Oct 2011 13:17:49 +0200, Simon wrote: On 2011/10/19 12:33 PM, Jerry Stuckle wrote: On 10/19/2011 5:30 AM, Simon wrote: RDBMS's can only use an index for a single column search if it's the first column in the index. For instance, if you had an index on "lname, fname", the RDBMS can use it for a search on lname, but not for one on fname. Thanks for the reply and explanation. It would then make sense to create an index for each and every possible select, (and also to _not_ use LIKE '%...%'). In my case, that would mean 13 new indexes. Do you think that the design is flawed in the first place? Should that information be in a different table structure? It depends. Lots of index can be a drawback if you're updating or inserting data, because each index may have to be updated as well. So, inserting a lot of records may take 20 or 30 times as long to do as not having those indexes. |
|
However, lots of indexes may not be a big deal if you aren't inserting a lot of data all the time. If you only insert 20 records a day, who gives a crap if it takes 0.003 seconds or it takes 0.124 seconds per? On the rare occasions when you DO have tens of thousands of records to put in, you can unhook the indexes, load the data, then re-add the indexs and they get built only once instead of getting adjusted for every one. You might also consider what people are actually searching FOR. Are they looking for states because they have legal or jurisdictional reasons to? Are they looking for location data for mailing? Are they looking to find out what's close by? If people mostly only care what's close, you may want to rethink searching by state, city, and zipcode entirely and switch to a spatial process instead: find a nice way to get coordinates for addresses and store and index that instead of the legal names of places. -- |
![]() |
| Thread Tools | |
| Display Modes | |
| |