简体   繁体   中英

How can I optimize large table in mysql?

I have a table with nearly 30 M records and size is 6.6 GB. I need to query some data from it and use group by and order by. It takes me too long to query the data, I lost connection to DB so many times...

I have index on all necessary fields as key and composite key. What else can I do to make it faster for the query?

Example query:

select id, max(price), avg(order) from table group by id, date order by id, location.

use EXPLAIN query , where query is your query. For example: EXPLAIN select * from table group by id, date order by id, location .

You'll see a table where MySQL analyses your query and shows which indices it looks for. Possibly you don't have sufficient (god enough) indices.

I don't think you can. With no filter (WHERE clause) and AVG the entire tables has to be read.

The only thing I can think of is to have a new table with ID, AVG_ORDER, MAX_PRICE (or whatever you need) and update that using a trigger or stored procedure when you insert/update new rows.

an index on ID,PRICE index might help you if you didn't need that pesky average.

Indexing isn't going to do you any good. You're averaging a column, so you have to read every row in the table. That's going to take time.

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