How to make my MySQL SUM() query more faster
-
I have about 1 million rows on users table and have columns A AA B BB C CC D DD E EE F FF by example to count int values 0 & 1 SELECT CityCode,SUM(A),SUM(B),SUM(C),SUM(D),SUM(E),SUM(F),SUM(AA),SUM(BB),SUM(CC),SUM(DD),SUM(EE),SUM(FF) FROM users GROUP BY CityCode Result 8 rows in set (24.49 sec). How to make my statement more faster?
-
Answer:
Use http://dev.mysql.com/doc/refman/5.1/en/using-explain.html to to know the excution plan of your query. Create atleast one or more Index. If possible make CityCode primary key. Try this one SELECT CityCode,SUM(A),SUM(B),SUM(C),SUM(D), SUM(E),SUM(F),SUM(AA),SUM(BB),SUM(CC),SUM(DD),SUM(EE),SUM(FF) FROM users GROUP BY CityCode,A,B,C,D,E,F,AA,BB,CC,DD,EE,FF
emma at Stack Overflow Visit the source
Other answers
Create an index on the CityCode column.
Xint0
I believe it is not because of SUM(), try to say select CityCode from users group by CityCode; it should take neary the same time...
Good.Dima
Use better hardware increase caching size - if you use InnoDB engine, then increase the innodb_buffer_pool_size value refactor your query to limit the number of users (if business logic permits that, of course)
Tudor Constantin
You have no WHERE clause, which means the query has to scan the whole table. This will make it slow on a large table. You should consider how often you need to do this and what the impact of it being slow is. Some suggestions are: Don't change anything - if it doesn't really matter Have a table which contains the same data as "users", but without any other columns that you aren't interested in querying. It will still be slow, but not as slow, especially if there are bigger ones (InnoDB) use CityCode as the first part of the primary key for table "users", that way it can do a PK scan and avoid any sorting (may still be too slow) Create and maintain some kind of summary table, but you'll need to update it each time a user changes (or tolerate stale data) But be sure that this optimisation is absolutely necessary.
MarkR
Related Q & A:
- How To Make Nokia X2 Faster?Best solution by askmefast.com
- How To Make Hair Grow Faster?Best solution by Yahoo! Answers
- How to make only ONE Sql query?Best solution by Stack Overflow
- How to do a MySQL recursive query?Best solution by Stack Overflow
- How to make Query read only?Best solution by Stack Overflow
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.