How do I reduce MySQL query execution time from 90s to less than 5s or even ONE?
-
I am using MySQL database. When I execute a query which the main table(20 thousands of rows) is joined with the other 5 tables, it takes 90 seconds to return me the result. I then added indices into each of the criteria(where clause). Then the execution time drops from 90 seconds to 45 seconds and then I tried to select only necessary fields instead of all(*), it takes 25 seconds. What should I do to make it drop to less than 5 seconds or even one second?
-
Answer:
Optimizing MySQL queries is a fairly straightforward process. To solve your problem would require a lot of information not provided about the structure of the database, the configuration of the server engine and storage arcitecture, load and contention, database & table structure, and network latency on te full round trip, for starters. That said, here's the most basic things to consider: A 20k row table is quite small. Sub-second results in 6-way joins with million-row tables should be closer to expectations. 1. list tables with smallest expected results first in the 'from' clause 2. avoid functions and computations in where clauses 3. avoid 'distinct', the 'like' operator, and outer joins 4. use 'explain' and 'explain extended' to insure you have the right indexes and your correlated sub-queries are cacheable 5. if the optimizer is not cooperating (mysql does not support hints), select the smallest two-table result set into a temporary table, and then join against that denormalized result. Basically, the idea is to reduce the number of bytes that need to be read & written to resolve the query, and the amount of nested result-set parsing. These 5 steps will go a long way towards reducing the i/o footprint of most multi-table queries.
Juan Cristián Vera Huneeus at Quora Visit the source
Other answers
I undertake performance optimizations on a daily basis with clients. Some optimization are easy, some are not. You need to know your tools such as EXPLAIN, CREATE TABLE, SHOW INDEXES, I_S and you need to understand the iterative process of verification. Recently I turned a 10 table join for a client that run 15,000 times per second across 100s of servers. They had invested a great amount of time to create indexes, and indeed all 10 tables were using indexes. Creating only better indexes, no code changes, no configuration changes, I was able to reduce the time 175ms to 10ms. Creating Indexes is only part of the process of query optimization. Knowing how to create better indexes is a topic for an upcoming talk at http://effectiveMySQL.com
Ronald Bradford
1. Read about the normalization of RDBMS 2. check if you have primary key defined for each table 3. check if you have foreign key defined for each relation between tables tables 4. if you do not have foreign key for one of the relations, think about whether your model is correct 5. if you want to understand you problem - Imagine two tables, each with 20,000 rows, without index. Then you can easily calculate that MySQL have to test 20.000 * 20.000 = 400.000.000 combinations :-(
Jiri Pallas
It can be of either not properly indexed, and even if you have indexes, the index is not used properly. Check this link http://www.ezeestudy.com/2010/06/22/mysql-query-optimization-3/, where it clearly explains on how you can optimize your queries
Deivanaathan A Krishnan
Try using a series of simpler queries - I've sped up similar queries by breaking down the joins and using large 'IN (...)' statements instead. This change alone sped up an address search using wildcard matches from over 30 seconds to just under 0.5 seconds.
Jacob Wyke
Never use Indexes on all the fields that you are using in WHERE clause.Try EXPLAIN YOUR MYSQL QUERY . This command will tell you about the no. of rows being scanned and whether your indexes are being used or not. To optimize a query, we need to avoid full table scans and reduce the no of rows being scanned. You got 20k rows with 6 joins and it takes about 25secs. Make sure you have your indexes on the correct fields (ex.scanning 2k over 20k is always going to be quicker). A row with just 20 records might not need indexes. Use your indexes wisely since it slows down writing your data .Clauses like GROUP BY, ORDER BY, LIKE ,DISTINCT are relatively more time consuming. If you are using them try evaluating it while scripting. Do not use sub-queries. Do not compute inside your query, evaluate it earlier if possible.If you have decent RAM, try caching your data if doesnt changes that frequently.Eventually, it will be a lot of trial and error to reach an optimized query. Also, your hardware matters. The more power it has ,the faster execution it does.I used to PARTITION my mysql table with great effect. See if you can use partition in your use case.In short : Sensible indexing + relevant caching + partitioning(if required) should solve your problem. 20K is still a very short no.
Jayesh Jain
Thereâs no way to know for sure unless you tell us whatâs the query.But let me give you some pointers. First of all make sure you add indexes to all the fields http://stackoverflow.com/questions/3567981/how-do-mysql-indexes-workThis will improve your query performance 10x fold.2. Make sure youâre using INNER JOIN instead of LEFT or RIGHT JOIN. INNER JOIN is faster than the others.20k rows is really not that much so I guess is an indexing issue.
Daniel Angel
Related Q & A:
- How could I reduce the length of the code?Best solution by Code Review
- How to make a MySql query faster?Best solution by Stack Overflow
- How do I reduce the font on My Yahoo page?Best solution by Yahoo! Answers
- How can I reduce font size?Best solution by Stack Overflow
- How do I reduce lines under my eyes?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.