Perform joins carefully
Joining data from two tables in one query is a very powerful
technique, particularly when combined with normalisation. However, if
not done right - even by a small margin - you can incur a serious speed
hit. To get good performance from your joins, follow these simple rules:
-
If two fields contain identical information in different tables, declare them with the same name and with the same type
-
Filter the query as best as you can, otherwise you can get a very
large number of results. For example, joining just three tables of 50
rows each will produce 125,000 records (50x50x50), as MySQL will return
every combination of the rows.
-
Remember that it is sometimes better to have a little data
duplication in exchange for the chance to not have slow joins. If your
goal is maximum speed, be prepared to break a few rules!
-
Try to use numbers as opposed to strings when comparing rows in
joins - the last thing you want is thousands of strings being compared.
-
Avoid joining rows where you are comparing non-indexed fields
No comments:
Post a Comment