简体   繁体   中英

mysql sometimes slow when join with large table

I have this large User table with 1 million plus rows. The strange thing is it queries normally for most of the users data, but always take up to 30 seconds for when query for some Users data. It doesn't happen randomly, but always on the particular User(s).

The query is like:

SELECT * FROM visit V
INNER JOIN group G ON G.id = V.group_id
INNER JOIN user U ON U.id = V.user_id AND U.group_id = V.parent_group_id
WHERE V.id = xxx AND V.group_id = xxx

But if I remove the JOIN with the table user, the query is normal again.

Does anyone knows how to solve this problem?

(The query is minified, the actual query is very long and also need to be kept confidential)

Joining large datasets requires indices on the JOIN and the WHERE clauses.

SELECT * FROM visit V
INNER JOIN group G ON G.id = V.group_id
INNER JOIN user U ON U.id = V.user_id AND U.group_id = V.parent_group_id
WHERE V.id = xxx AND V.group_id = xxx

To make the join run efficiently, the following fields MUST be defined as Indices for those tables:

  • V.group_id
  • V.user_id
  • V.parent_group_id
  • U.group_id

To make the query run efficiently, it should not use SELECT * (specify only the columns you need), and the query should be optimized

We can't help if we can't see your data and code.

Ultimately, you're asking a community to help you solve a MySQL performance issue on a large data-set, but are unwilling or unable to show us any of the table-structure, the full-query, or sample source-data and desired output.

As long we we're flying blind, there's nothing we can do to help.

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