Domanda

Please I need a help. I have a query I run to fetch data from two tables, but its consuming a lot of memory it takes as much as 9 seconds to fetch about 100 records. I may need to fetch up to 200k records at some point. Please how do I optimize this query?

  SELECT user_id AS id, name, address, phone, city, book_title 
  FROM users us LEFT OUTER JOIN 
  (SELECT bid, GROUP_CONCAT(DISTINCT book_title SEPARATOR ' ') 
   AS book_title FROM user_books GROUP BY bid)bo ON bo.bid = us.user_id

On the user_books table I have multiple entries of book_titles for some users, I want to return a result that includes all the users both with a book title or not and the book_title in one column.

I ran an sql explain statement for the query and got this

id | select_type  | table   | type | possible_keys | key | key_len | ref | rows | Extra
1     PRIMARY      us         ALL      NULL          NULL   NULL     NULL  168272
1     PRIMARY     <derived2>  ALL      NULL          NULL   NULL     NULL  65900
2     DERIVED     user_books  ALL      NULL           uid   4        NULL  164989 

Note: I have an index for columns bid, book_title, user_id, name, address, phone, city in their respective tables.

Please any suggestions on how to boost the performance.

È stato utile?

Soluzione

Why you need to create derived table? why not this way:

SELECT 
user_id, name, address, phone, city, GROUP_CONCAT(DISTINCT book_title SEPARATOR ' ')  book_title 
FROM 
users us 
LEFT OUTER JOIN user_books ub ON ub.bid = us.user_id
GROUP BY us.user_id

Altri suggerimenti

try this - much more performant than the other answer given here. Doing it this way prevents a lot of overhead in determining the grouping for each user_id, and you can always just add a WHERE clause to the selection from the users table if required.

SELECT
*,
  ( SELECT GROUP_CONCAT(DISTINCT book_title SEPARATOR ' ')
    FROM user_books ub
    WHERE ub.bid=us.id
  ) AS book_title
FROM
 ( SELECT user_id AS id, name, address, phone, city, book_title 
   FROM users
 ) us
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top