简体   繁体   中英

database query optimization mySQL

Hello was wondering what the better approach is: the target database will only have about 100,000 rows

To have one table like below:

AllCars

ID | Car_Name |    Car_Model    | Car_Type  | Car_Features | Car_Location
______________________________________________________________________
1  |  Nissan  | Ultima Explorer |   4D      | Airbag Power | TX

SQL: Select 'X' from AllCards where Car_Model LIKE '%ultima%';

or to break it up into many tables

Car_Types

ID | Car_Model |
----------------
1  | Nissa Ultima |

and write joins:

SQL:

   SELECT'X' from Car_Types c 
     JOIN AllCars a 
     ON a.ID = c.ID  
     WHERE Car_Model = 'Ultima';

I'm confused. The two queries are not the same. In one case:

where Car_Model LIKE '%ultima%'

And in the other:

WHERE Car_Model = 'Ultima'

If you think that 'Ultima' is a valid value for car_model , I would suggest that you store 'Ultima' in the field, with nothing else. Perhaps you should have a multi-part identifier for the model.

In any case, it doesn't make a big difference whether you store the raw data in one table or whether you use a reference table -- so long as you store the value consistently. The "consistently" often leads to using a reference table approach, which would be the second approach. The key, though, is separating out the different components so you can use equality for the comparison rather than wildcards.

It is almost always better to normalize your data. However, you must ensure that you are not performing joins for even the most frequent queries.

There is no right or wrong answer to your question. It all depends on your use case, what kind of data you are going to store, how frequent it will be queried and how it would be queried, meaning what are the fields queried for most frequently. So it would be better if you would provide more details about the nature and frequency of your queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM