我有三个执行 COUNT 的 SELECT 语句。认为结合这些在mysql上会更有效和更友好:
$query = $db->query("SELECT count(category) FROM table WHERE cid='290463' and category = 'x'");
$tot_x = $db->result($query, 0);
$query = $db->query("SELECT count(category) FROM table WHERE cid='290463' and category = 'y'");
$tot_y = $db->result($query, 0);
$query = $db->query("SELECT count(category) FROM table WHERE cid='290463' and category = 'z'");
$tot_z = $db->result($query, 0);
一句话:
SELECT
SUM(category='x' AND cid='290463') as tot_x,
SUM(category='y' AND cid='290463') as tot_y,
SUM(category='z' AND cid='290463') as tot_z
FROM table
问题是,新语句比单独运行原来的三个语句要慢。
任何人都能够阐明为什么新语句较慢以及改进 SELECT 以使其更快的建议?