我如何创建一个和php函数类似的过程(优化加速)
我有一张像下面这样的桌子我如何创建一个和php函数类似的过程(优化加速),php,mysql,stored-procedures,mariadb,mariasql,Php,Mysql,Stored Procedures,Mariadb,Mariasql,我有一张像下面这样的桌子 mysql> select * from dts; +----+------+------+--------+------+------+------+------+------+ | Id | key1 | key2 | serial | pr1 | pr2 | pr3 | pr4 | pr5 | +----+------+------+--------+------+------+------+------+------+ | 1 | 1 |
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)
我想将key1、key2、db、table和字段列表以逗号分隔传递给procedure,并想从这些字段中搜索select语句返回的记录的非零值,如果假设我得到所有非零值的字段,只需断开循环并返回字符串即可
下面我尝试使用php
function show_available($key1, $key2, $db, $table, $conn, $fields=null)
{
/* Select all fields in argument from db table where key... */
$query = "select ".$fields." FROM $db.$table where key1=$key1 and key2=$key2";
/* Query */
$result = $conn->query($query , MYSQLI_USE_RESULT);
/* Output array */
$out = array();
while($row=$result->fetch_assoc())
{
/* Loop through fields */
foreach($row as $key => $val)
{
/* If val is greater than 0*/
if($val > 0 ){
/*Ok we got field which has value greater than 0*/
$out[$key]=1;
}
}
/* If all fields are found ok in so many records where key1=x and key2=x, break loop */
if(count($out) == count($field_arr))break;
}
/* Return which all fields has value greater than 0 */
return implode(',', array_keys($out));
}
在相同的功能,我想转换成程序,以加快我的任务,并希望有如下输出,如何可能,请帮助别人
如果我传递一些过程1,1,db,table,'pr1,pr2,pr3,pr4,pr5',我想得到输出pr1,pr3,pr4,pr5,因为当key1=1和key2=1时
对于键1=2和键2=2,情况类似
| 8 | 2 | 2 | 1 | 1 | 1 | 1 | 1 | 2 | - Found pr1-pr5, break loop and return string
| 9 | 2 | 2 | 2 | 0 | 0 | 0 | 0 | 0 |
预期产量
# For procedure call expected o/p
key1 key2 fields_non_zero
1 1 pr1,pr3,pr4,pr5
1 2 pr2,pr4,pr5
2 2 pr1,pr2,pr3,pr4,pr5
3 2
3 3 pr1,pr2,pr3,pr4,pr5
表转储
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;
您需要的是使用max进行聚合,或使用concat_ws进行求和:
我不知道你在问什么,上面的代码似乎传递回了数据。我想创建过程,我不知道如何在mysql中创建它,我可以编写phpfunction@user3637224-您可以直接使用上述SQL或查看。您可以在过程中使用上述查询并返回结果。但它不会中断循环,对于LAC行,如果使用sum或max,则速度会很慢。@user3637224-如果您认为循环会更快,那么您就错了。最快的解决方案通常是基于SQL的,而不是过程性的。我是否可以花更多的时间检查我的数据,选择countdistinct key1,key2是541665?表总计数为113441280
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;
select
key1, key2,
concat_ws(
',',
case when max(pr1) <> 0 then 'pr1' end,
case when max(pr2) <> 0 then 'pr2' end,
case when max(pr3) <> 0 then 'pr3' end,
case when max(pr4) <> 0 then 'pr4' end,
case when max(pr5) <> 0 then 'pr5' end
) as val
from dts
group by key1, key2;
key1 key2 val
1 1 pr1,pr3,pr4,pr5
1 2 pr2,pr4,pr5
2 2 pr1,pr2,pr3,pr4,pr5
3 2
3 3 pr1,pr2,pr3,pr4,pr5