gpt4 book ai didi

mysql查询优化和字段值操作

转载 作者:行者123 更新时间:2023-11-28 23:14:43 25 4
gpt4 key购买 nike

mysql> select * from dts;
+----+------+------+--------+------+------+------+------+------+
| Id | key1 | key2 | serial | pr1 | pr2 | pr3 | pr4 | pr5 |
+----+------+------+--------+------+------+------+------+------+
| 1 | 1 | 1 | 1 | 0 | 0 | 1 | 0 | 2 |
| 2 | 1 | 1 | 2 | 0 | 0 | 0 | 0 | 0 |
| 3 | 1 | 1 | 3 | 0 | 0 | 0 | 1 | 0 |
| 4 | 1 | 1 | 4 | 1 | 0 | 1 | 1 | 3 |
| 5 | 1 | 2 | 5 | 0 | 0 | 0 | 2 | 5 |
| 6 | 1 | 2 | 6 | 0 | 0 | 0 | 0 | 1 |
| 7 | 1 | 2 | 7 | 0 | 1 | 0 | 0 | 0 |
| 8 | 2 | 2 | 1 | 1 | 1 | 1 | 1 | 2 |
| 9 | 2 | 2 | 2 | 0 | 0 | 0 | 0 | 0 |
| 10 | 3 | 2 | 3 | 0 | 0 | 0 | 0 | 0 |
| 11 | 3 | 3 | 1 | 1 | 1 | 0 | 0 | 1 |
| 12 | 3 | 3 | 5 | 0 | 0 | 1 | 1 | 0 |
+----+------+------+--------+------+------+------+------+------+
12 rows in set (0.00 sec)

我这里要实现的逻辑如下

  1. check is there any non zero data in fields(pr1-pr5) of table dts ?
  2. If found concat field name with comma, suppose if all fields are non zero, just concat fields and stop reading records of same key (combination of key1,key2 to save execution time, look it 8th row in above table for key1=2 and key2=2 all are non zero, so stop reading next record with same keys), go to next key1,key2

不工作的如下

At present code which I am using is working, but its not skipping reading row when enough fields are found, as you can see for current example it creates 12 rows X 5 Col = 60 Rows (if you execute inner select statement it returns 60 rows) and then groups it by key1 and key2, can it be done in any simple way which is efficient too so that table with 2-3 million records will be faster.

预期输出

+------+------+---------------------+
| key1 | key2 | prs |
+------+------+---------------------+
| 1 | 1 | pr1,pr3,pr4,pr5 |
| 1 | 2 | pr2,pr4,pr5 |
| 2 | 2 | pr1,pr2,pr3,pr4,pr5 |
| 3 | 2 | NULL |
| 3 | 3 | pr1,pr2,pr3,pr4,pr5 |
+------+------+---------------------+

那么如何简化这可以提高大表的性能,正如你在上面看到的那样我的意图很简单,我只想看看每个组合有多少字段 (pr1-pr5) 存在非零值key1, key2

正在使用的代码

SELECT 
key1,
key2,
group_concat(distinct case when val > 0 then pr end order by pr separator ',') prs
FROM (
SELECT
d.key1,
d.key2,
t.pr,
CASE t.pr
WHEN 'pr1' THEN pr1
WHEN 'pr2' THEN pr2
WHEN 'pr3' THEN pr3
WHEN 'pr4' THEN pr4
WHEN 'pr5' THEN pr5
END val
FROM
dts d
CROSS JOIN (
SELECT 'pr1' pr UNION ALL
SELECT 'pr2' UNION ALL
SELECT 'pr3' UNION ALL
SELECT 'pr4' UNION ALL
SELECT 'pr5'
) t
) r
GROUP BY key1 , key2;

结构

DROP TABLE IF EXISTS `dts`;
CREATE TABLE `dts` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`key1` int(11) DEFAULT '-99',
`key2` int(11) DEFAULT '-99',
`serial` int(11) DEFAULT '-99',
`pr1` int(11) DEFAULT '-99',
`pr2` int(11) DEFAULT '-99',
`pr3` int(11) DEFAULT '-99',
`pr4` int(11) DEFAULT '-99',
`pr5` int(11) DEFAULT '-99',
PRIMARY KEY (`Id`),
KEY `main` (`key1`,`key2`,`serial`)
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;


LOCK TABLES `dts` WRITE;
INSERT INTO `dts` VALUES (1,1,1,1,0,0,1,0,2),(2,1,1,2,0,0,0,0,0),(3,1,1,3,0,0,0,1,0),(4,1,1,4,1,0,1,1,3),(5,1,2,5,0,0,0,2,5),(6,1,2,6,0,0,0,0,1),(7,1,2,7,0,1,0,0,0),(8,2,2,1,1,1,1,1,2),(9,2,2,2,0,0,0,0,0),(10,3,2,3,0,0,0,0,0),(11,3,3,1,1,1,0,0,1),(12,3,3,5,0,0,1,1,0);
UNLOCK TABLES;

Blockquote

最佳答案

SELECT key1, key2,
CONCAT_WS(',',
IF(pr1=0, NULL, 'pr1'),
IF(pr2=0, NULL, 'pr2'),
IF(pr3=0, NULL, 'pr3'),
IF(pr4=0, NULL, 'pr4'),
IF(pr5=0, NULL, 'pr5') ) AS prs
FROM (
SELECT key1, key2,
SUM(pr1) AS pr1,
SUM(pr2) AS pr2,
SUM(pr3) AS pr3,
SUM(pr4) AS pr4,
SUM(pr5) AS pr5
FROM dts
GROUP BY key1, key2
) AS sums;

如果你需要3,2行中的“NULL”,你可以添加一个IFNULL来修复它。

关于mysql查询优化和字段值操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44393184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com