mysql - Using JOIN'ed mapping tables better than multiple fields in the same table? -


i have table of 360,000 records , here's performing query on 2 indexed fields:

select count(*) emails department_id in(1,2,3,4) , category_id in (5,6,7,8)  (time: 0.9624802)             id: 1   select_type: simple         table: emails          type: range possible_keys: emails_department_id_idx,emails_category_id_idx           key: emails_category_id_idx       key_len: 5           ref: null          rows: 54018         extra: using 

so 1 index being used there. (i can index merge work when using simpler comparisons or range criteria, need checks against list of ids).

here created 2 new tables map relationship, , using join's replicated same results:

select count(*) emails left join email_to_department on (email_to_department.email_id = emails.id , email_to_department.department_id in (1,2,3,4)) left join email_to_category on (email_to_category.email_id = emails.id , email_to_category.category_id in (5,6,7,8)) email_to_department.department_id not null , email_to_category.category_id not null  (time: 0.5217777)  *************************** 1. row ***************************            id: 1   select_type: simple         table: email_to_category          type: range possible_keys: primary,category_id           key: category_id       key_len: 4       ref: null      rows: 61282     extra: using where; using index *************************** 2. row ***************************            id: 1   select_type: simple         table: email_to_department          type: ref possible_keys: primary,department_id           key: primary       key_len: 4           ref: testdb.email_to_category.email_id          rows: 1         extra: using where; using index *************************** 3. row ***************************            id: 1   select_type: simple         table: emails          type: eq_ref possible_keys: primary           key: primary       key_len: 4           ref: testdb.email_to_category.email_id          rows: 1         extra: using index 3 rows in set (0.38 sec) 

so each query uses index, , trims half time off. bad design? should write rest of these relationships in same way?

if add more criteria in same way, join'd version of query seems faster , other remains more or less same.

doing simple query on single indexed field very fast of course:

select count(*) emails department_id in(1,2,3,4) 

is there strategy might use make these kinds of queries faster still? there other properties need filtered on well, , in different combinations, creating multi-column indexes won't help.

this question database normalization. can information on topic in many places.

basic answer since there huge literature this, , there lot of differences, point out boils down trade-offs; speed versus storage requirements, or ease of use versus data duplication. may want understand normalization in order understand why or not want it.

further reading deep topic, may want learn more - there hundreds of books , thousands of scholarly papers on these issues. instance, @ previous question designing database: database design: 1 huge table or separate tables?, or one: first-time database design: overengineering? or database normalization basics on about.com.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -