简体   繁体   中英

Optimise mysql subquery for multiple columns

I have one show table which have 25 lakh records in it. Columns in show tables is id, showid, artist_id, cityid, created.

If same show is happening for a period of 4 days. Then in system data will be like :

id, showid, artist_id, cityid, created for each show, showid will remain same.

1 245 1 3 2011-12-30 15:00:00

2 245 1 3 2011-12-30 13:00:00

3 245 1 3 2011-12-30 19:00:00

SELECT UT.user_id,UT.type_id, UT.type ,UT.track_time, 
(SELECT showid FROM shows WHERE FIND_IN_SET(UT.type_id,artist_id) ORDER BY created DESC LIMIT 1)   as mainshowid,
(SELECT cityid FROM shows WHERE FIND_IN_SET(UT.type_id,artist_id) ORDER BY created DESC LIMIT 1)   as maincityid
FROM user_details as UT  WHERE UT.type='artist' ORDER BY UT.track_time desc limit 20;

Query is giving the desire output But how i can get multiple columns in single SUB query or by any other way.

You can:

SELECT 
    UT.user_id,
    UT.type_id, 
    UT.type,
    UT.track_time, 
    s.showid AS mainshowid,
    s.cityid AS maincityid
FROM 
      user_details AS UT 
  JOIN
      shows AS s
    ON s.showid =
       ( SELECT showid 
         FROM shows 
         WHERE FIND_IN_SET(UT.type_id, artist_id)
         ORDER BY created DESC 
         LIMIT 1
       ) 
WHERE UT.type='artist' 
ORDER BY UT.track_time DESC 
LIMIT 20 ;

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