Reputation: 51
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?
Upvotes: 2
Views: 8343
Reputation: 63538
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:
But be sure that this optimisation is absolutely necessary.
Upvotes: 0
Reputation: 4219
Use explain 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
Upvotes: 2
Reputation: 26861
innodb_buffer_pool_size
valueUpvotes: 1
Reputation: 2123
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...
Upvotes: 2