I have three SELECT statements that do COUNTs. Thought it would be more efficient and kinder on mysql to combine these:
$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);
into one statement:
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
Problem is, the new statement is slower than running the original three separately.
Anyone able to shed light on why the new statement is slower and also recommendations to improve the SELECT to be faster?