我在 php 中使用 PDO for Mysql 遇到了一个非常奇怪的问题。
我有一张桌子,里面有大约 30,000 行。
如果我尝试运行此查询:
SELECT card_data.name, max(card_data.set_order) FROM card_data, theme_decks WHERE card_data.name = 'Mana Leak' and card_data.enabled = 1 GROUP BY card_data.name ORDER BY card_data.name
(这个怎么做的)
SELECT card_data.name, max(card_data.set_order) FROM card_data, theme_decks
WHERE card_data.name = ? and
card_data.enabled = 1 GROUP BY card_data.name ORDER BY card_data.name ASC
$paramArray = array("Mana Leak");
$sth->execute($paramArray);
通过 PDO 执行大约需要 10 秒!但是,如果我通过 phpmyadmin 运行相同的查询,则只需不到一秒钟的时间..
奇怪的是,下面的这个查询虽然 PDO 只需要不到一秒钟的时间:
SELECT card_data.name, max(card_data.set_order) FROM card_data, theme_decks WHERE
(card_data.name = 'Extraplanar Lens' OR card_data.name = 'Mana Leak' OR card_data.name =
'overgrowth') and card_data.enabled = 1 GROUP BY card_data.name ORDER BY card_data.name ASC
(这个怎么做的)
SELECT card_data.name, max(card_data.set_order) FROM card_data, theme_decks WHERE
(card_data.name = ? OR card_data.name = ? OR card_data.name =
?) and card_data.enabled = 1 GROUP BY card_data.name ORDER BY card_data.name ASC
$paramArray = array("Extraplanar Lens","Mana Leak","overgrowth");
$sth->execute($paramArray);
我一生都无法锻炼,为什么搜索单个“名称”需要 10 秒,而搜索多个名称只需要大约 0.4 秒...
编辑:情节变厚。我删除了参数化语句并硬编码了要搜索的文本
SELECT card_data.name, max(card_data.set_order) FROM card_data, theme_decks WHERE
card_data.name = Mana Leak and card_data.enabled = 1 GROUP BY card_data.name ORDER BY
card_data.name ASC
使用 PDO 查询需要 0.000384092330933 秒!
但
SELECT card_data.name, max(card_data.set_order) FROM card_data, theme_decks WHERE
card_data.name = 'Mana Leak' and card_data.enabled = 1 GROUP BY card_data.name ORDER BY
card_data.name ASC
用 PDO 查询需要 11.00254 秒!