I want to create a database structure that will has good efficiency. Now database looks like that:
data (type:primary; data is unique because miliseconds) | category | price | plenty different columns of data
201201231100121234 | 3 | 0 |
201201231100121249 | 14 | 5 |
-
Let say I have about 15 main categories so I thought to keep every record in specific table, depends from category. So if a user will do a search in specific category then query will come to only for all records in specific table. So it is good solution, right?
-
But what if user wants to see all records from all categories sort by data? Probably I need to use UNION, but the speed search using a temporary table will be a lot worse than keep all categories in one table, right? Probably I allow user search only the chosen category, not in all categories.
-
The last question is about sort. When a user uses sort with “data” column like that: “sort by data asc LIMIT 0,10” or “sort by data desc LIMIT 0,10” then search is fast, because data is unique. But when user wants to sort by price (that can’t be unique) then there is no solution to the query can has more performance? Any solution for that?
No – this is a bad idea, it places a lot more complexity on the code managing the data and doesn’t give any measurable performance benefit compared with a properly indexed table. Certainly there might be a case for varying the structure if different categories have different sets of fields associated with them – but this would be best represented by maintaining the common field set in a single table with the category specific fields in other tables using a 1:1 non-oblig join.
Your 3rd point seems a bit confused. There’s nothing to stop you adding indexes on other columns, however, using your example, suppose you’ve got 20 records each with the max value for price – which ones do you expect to see when you
SORT BY price DESC LIMIT 0,10?