这是我的查询:
SELECT DISTINCT (game.game_id)
AS gid, game_link.is_linked, game_link.game_id1, game_bet_settings.bet_type_id
AS bet_type
FROM game
LEFT JOIN kiosk_to_game ON ( kiosk_to_game.game_id = game.game_id )
LEFT JOIN game_bet_settings ON ( game_bet_settings.game_id = game.game_id )
LEFT JOIN game_link ON ( game_link.game_id1 = game.game_id
OR game_link.game_id2 = game.game_id )
AND game.game_id != ''
AND game.game_winner = '0'
AND game.game_open_bet_time <= '2014-02-21 12:12:48'
AND game.game_close_bet_time >= '2014-02-21 12:12:48'
AND kiyosuku_to_game.kiyosuku_id = '1'
AND game.game_is_active = '1'
AND game_bet_settings.is_disabled !=1
AND (game_link.is_linked IS NULL OR game_link.is_linked =1)
ORDER BY game.game_start_time ASC
我认为罪魁祸首可能是:
SELECT DISTINCT (game.game_id)
AS gid, game_link.is_linked, game_link.game_id1, game_bet_settings.bet_type_id
AS bet_type
任何人都可以将此查询简化为更高效的查询吗?
这是sql进程的图像: https://lh5.googleusercontent.com/--BsUDk8jLnY/UwbKFiCjZCI/AAAAAAAAAi4/MXCQFhs4PeU/w496-h216-no/sqlquery.png
这是我的一些表索引:
游戏
PRIMARY - game_id
GAME_LINK
PRIMARY - game_link_id
game_id1 - game_id1, game_id2
game_bet_settings
PRIMARY - game_bet_settings_id
game_id - game_id
答案 0 :(得分:2)
您可以尝试添加此索引:
game(game_winner, is_active, open_bet_time, close_bet_time, game_id);
然而,罪魁祸首可能是:
LEFT JOIN game_link ON ( game_link.game_id1 = game.game_id OR game_link.game_id2 = game.game_id )
or
的查询难以优化。
尝试在没有这种条件的情况下运行查询:
SELECT DISTINCT (game.game_id)
AS gid, game_link.is_linked, game_link.game_id1, game_bet_settings.bet_type_id
AS bet_type
FROM game
LEFT JOIN kiosk_to_game ON ( kiosk_to_game.game_id = game.game_id )
LEFT JOIN game_bet_settings ON ( game_bet_settings.game_id = game.game_id )
LEFT JOIN game_link ON ( game_link.game_id1 = game.game_id )
AND game.game_id != ''
AND game.game_winner = '0'
AND game.game_open_bet_time <= '2014-02-21 12:12:48'
AND game.game_close_bet_time >= '2014-02-21 12:12:48'
AND kiyosuku_to_game.kiyosuku_id = '1'
AND game.game_is_active = '1'
AND game_bet_settings.is_disabled !=1
AND (game_link.is_linked IS NULL OR game_link.is_linked =1)
ORDER BY game.game_start_time ASC
如果这在合理的时间内有效,那么尝试将两个查询联合起来(每个条件一个)。
编辑:
工会看起来像:
(SELECT DISTINCT (game.game_id)
AS gid, game_link.is_linked, game_link.game_id1, game_bet_settings.bet_type_id
AS bet_type
FROM game
LEFT JOIN kiosk_to_game ON ( kiosk_to_game.game_id = game.game_id )
LEFT JOIN game_bet_settings ON ( game_bet_settings.game_id = game.game_id )
LEFT JOIN game_link ON ( game_link.game_id1 = game.game_id )
AND game.game_id != ''
AND game.game_winner = '0'
AND game.game_open_bet_time <= '2014-02-21 12:12:48'
AND game.game_close_bet_time >= '2014-02-21 12:12:48'
AND kiyosuku_to_game.kiyosuku_id = '1'
AND game.game_is_active = '1'
AND game_bet_settings.is_disabled !=1
AND (game_link.is_linked IS NULL OR game_link.is_linked =1)
)
UNION
(SELECT DISTINCT (game.game_id)
AS gid, game_link.is_linked, game_link.game_id1, game_bet_settings.bet_type_id
AS bet_type
FROM game
LEFT JOIN kiosk_to_game ON ( kiosk_to_game.game_id = game.game_id )
LEFT JOIN game_bet_settings ON ( game_bet_settings.game_id = game.game_id )
LEFT JOIN game_link ON ( game_link.game_id2 = game.game_id )
AND game.game_id != ''
AND game.game_winner = '0'
AND game.game_open_bet_time <= '2014-02-21 12:12:48'
AND game.game_close_bet_time >= '2014-02-21 12:12:48'
AND kiyosuku_to_game.kiyosuku_id = '1'
AND game.game_is_active = '1'
AND game_bet_settings.is_disabled !=1
AND (game_link.is_linked IS NULL OR game_link.is_linked =1)
)
ORDER BY game_start_time ASC
答案 1 :(得分:1)
我建议将查询放置条件重新组合到子查询以限制记录数量。
SELECT DISTINCT (game.game_id)
AS gid, game_link.is_linked, game_link.game_id1, game_bet_settings.bet_type_id
AS bet_type
FROM (select * from game
where
game.game_id != ''
AND game.game_winner = '0'
AND game.game_open_bet_time <= '2014-02-21 12:12:48'
AND game.game_close_bet_time >= '2014-02-21 12:12:48'
AND game.game_is_active = '1'
)
LEFT JOIN kiosk_to_game ON ( kiosk_to_game.game_id = game.game_id )
LEFT JOIN game_bet_settings ON ( game_bet_settings.game_id = game.game_id )
LEFT JOIN game_link ON ( game_link.game_id1 = game.game_id
OR game_link.game_id2 = game.game_id )
AND kiyosuku_to_game.kiyosuku_id = '1'
AND (game_link.is_linked IS NULL OR game_link.is_linked =1)
ORDER BY game.game_start_time ASC