Specific Query Performance Tips
- Use EXPLAIN to profile the query execution plan
- Use Slow Query Log
- Don't use DISTINCT when you have or could use GROUP BY
- Insert performance
- Batch INSERT and REPLACE
- Use LOAD DATA instead of INSERT
- LIMIT m,n may not be as fast as it sounds. Learn how to improve it and read more about Efficient Pagination Using MySQL
- Don't use ORDER BY RAND() if you have > ~2K records
- Use SQL_NO_CACHE when you are SELECTing frequently updated data or large sets of data
- Avoid wildcards at the start of LIKE queries
- Avoid correlated subqueries and in select and where clause (try to avoid in)
- No calculated comparisons -- isolate indexed columns
- ORDER BY and LIMIT work best with equalities and covered indexes
- Separate text/blobs from metadata, don't put text/blobs in results if you don't need them
- Derived tables (subqueries in the FROM clause) can be useful for retrieving BLOBs without sorting them. (Self-join can speed up a query if 1st part finds the IDs and uses then to fetch the rest)
- ALTER TABLE...ORDER BY can take data sorted chronologically and re-order it by a different field -- this can make queries on that field run faster (maybe this goes in indexing?)
- Know when to split a complex query and join smaller ones
- Delete small amounts at a time if you can
- Make similar queries consistent so cache is used
- Have good SQL query standards
- Don't use deprecated features
- Turning OR on multiple index fields (<5.0) into UNION may speed things up (with LIMIT), after 5.0 the index_merge should pick stuff up.
- Don't use COUNT * on Innodb tables for every search, do it a few times and/or summary tables, or if you need it for the total # of rows, use SQL_CALC_FOUND_ROWS and SELECT FOUND_ROWS()
- Use INSERT ... ON DUPLICATE KEY update (INSERT IGNORE) to avoid having to SELECT
- use groupwise maximum instead of subqueries
- Avoid using IN(...) when selecting on indexed fields, It will kill the performance of SELECT query.
- Prefer using UNION ALL if you don't need to merge the resul
Posted By : Mr. Joydeep Das
No comments:
Post a Comment