简体   繁体   English

为多个列优化mysql子查询

[英]Optimise mysql subquery for multiple columns

I have one show table which have 25 lakh records in it. 我有一个节目表,里面有250万条记录。 Columns in show tables is id, showid, artist_id, cityid, created. 节目表中的列是id,showid,artist_id,cityid,已创建。

If same show is happening for a period of 4 days. 如果同一场演出正在进行4天。 Then in system data will be like : 然后在系统中的数据将像:

id, showid, artist_id, cityid, created for each show, showid will remain same. 为每个节目创建的ID,ShowID,Artist_ID,CityID,ShowID将保持不变。

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

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

3 245 1 3 2011-12-30 19: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. 查询提供了期望的输出,但是我如何在单个SUB查询中或通过任何其他方式获取多列。

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 ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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