简体   繁体   中英

How to speed up an sql query

I have a table which lists advertising actions by brands. Each action is recorded by one entry as shown in the excel file which you can see in the drive folder below. There are about 7mil records in the table. I am running several queries which run fine but i am facing an issue with one specific report. This report lists and ranks the clients (Column L in Excel file) by total amounts spent on advertising. This report takes about a minute to generate if the date range set is anywhere from two months to two years.

Below is a copy of the query

select customer_id, sum(value) as value 
from `data` 
where ((`date` >= '2019-01-01' and `date` <= '2019-12-31')) 
group by `customer_id` 
order by `value` desc, `customer_id` asc;

This report takes about a minute to generate if the date range set is anywhere from 2 months to two years. If my date range selection is for one month or less, it takes less than 3 seconds.

I need to get this query to process in less than 10-15 seconds max. We tried to come up with ideas such as creating a new table in the DB especially for this query but it hit a wall when we saw that we still had to keep all date records, and we could therefore not group the results in the table.

We really are open to any kind of idea that would make this query faster, including DB changes.

Below is the link to the folder which contains a copy of the DB structure and a sample data set exported from the data table which contains all the data.

Drive Folder

You should make an index on fields that are essential to the criteria of your query. In this case something like:

CREATE INDEX `idx_data_date` ON `data`(`date`);
CREATE INDEX `idx_data_customer_id` ON `data`(`customer_id`);

You should also avoid storing dates as text. Use DATETIME if you can.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM