Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不同条件下多个计数的mysql性能并集_Mysql_Sql - Fatal编程技术网

不同条件下多个计数的mysql性能并集

不同条件下多个计数的mysql性能并集,mysql,sql,Mysql,Sql,有一个表包含150万条记录,需要在网页上显示不同条件下的若干金额 假设表的结构如下: +-------+------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+

有一个表包含150万条记录,需要在网页上显示不同条件下的若干金额

假设表的结构如下:

+-------+------------+------+-----+-------------------+-----------------------------+
| Field | Type       | Null | Key | Default           | Extra                       |
+-------+------------+------+-----+-------------------+-----------------------------+
| id    | int(11)    | NO   | PRI | NULL              | auto_increment              |
| userId| int(11)    | YES  |     | NULL              |                             |
| cona  | tinyint(4) | YES  |     | NULL              |                             |
| conb  | tinyint(4) | YES  |     | NULL              |                             |
| conc  | tinyint(4) | YES  |     | NULL              |                             |
| date  | timestamp  | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+------------+------+-----+-------------------+-----------------------------+
SELECT count(DISTINCT userId) FROM test
UNION ALL
SELECT count(DISTINCT userId) FROM test WHERE cona=0
UNION ALL
SELECT count(DISTINCT userId) FROM test WHERE cona=1
UNION ALL
SELECT count(DISTINCT userId) FROM test WHERE conb=0
UNION ALL
SELECT count(DISTINCT userId) FROM test WHERE conb=1 AND conc=0
UNION ALL
SELECT count(DISTINCT userId) FROM test WHERE conb=1 AND conc>0 AND date IS NOT NULL AND NOW() > date
UNION ALL
SELECT count(DISTINCT userId) FROM test WHERE conb=1 AND conc=1 AND date IS NOT NULL AND NOW() <= date
UNION ALL
SELECT count(DISTINCT userId) FROM test WHERE conb=1 AND conc=2 AND date IS NOT NULL AND NOW() <= date
UNION ALL
SELECT count(DISTINCT userId) FROM test WHERE conb=2
需要查询不同条件的用户金额,如下:

+-------+------------+------+-----+-------------------+-----------------------------+
| Field | Type       | Null | Key | Default           | Extra                       |
+-------+------------+------+-----+-------------------+-----------------------------+
| id    | int(11)    | NO   | PRI | NULL              | auto_increment              |
| userId| int(11)    | YES  |     | NULL              |                             |
| cona  | tinyint(4) | YES  |     | NULL              |                             |
| conb  | tinyint(4) | YES  |     | NULL              |                             |
| conc  | tinyint(4) | YES  |     | NULL              |                             |
| date  | timestamp  | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+------------+------+-----+-------------------+-----------------------------+
SELECT count(DISTINCT userId) FROM test
UNION ALL
SELECT count(DISTINCT userId) FROM test WHERE cona=0
UNION ALL
SELECT count(DISTINCT userId) FROM test WHERE cona=1
UNION ALL
SELECT count(DISTINCT userId) FROM test WHERE conb=0
UNION ALL
SELECT count(DISTINCT userId) FROM test WHERE conb=1 AND conc=0
UNION ALL
SELECT count(DISTINCT userId) FROM test WHERE conb=1 AND conc>0 AND date IS NOT NULL AND NOW() > date
UNION ALL
SELECT count(DISTINCT userId) FROM test WHERE conb=1 AND conc=1 AND date IS NOT NULL AND NOW() <= date
UNION ALL
SELECT count(DISTINCT userId) FROM test WHERE conb=1 AND conc=2 AND date IS NOT NULL AND NOW() <= date
UNION ALL
SELECT count(DISTINCT userId) FROM test WHERE conb=2
但是费用和以前一样。
我真的没有sql优化方面的经验。

是否可以优化union sql?

尝试使用临时表,然后首先在临时表中插入所有数据,然后您现在可以操作临时表并应用所有条件。您是否使用某种方式与MySQL通信?