This question is about query optimization to avoid multiple calls to the database via PHP.
So, here is the scenario, I have two tables, one of which contains information that you can call a reference table, and the other - a data table, the key1 and key2 fields are common in both tables based on these fields. Join them.
I donβt know if the request can be made even simpler than what I am doing right now, what I want to achieve:
I would like to find an excellent key1,key2,info1,info2 from the main_info table, when the serial value is less than 10 and key1,key2 both tables, and then group them by info1,info2 , while grouping count duplicate key1,key2 for duplicate info1,info2 fields info1,info2 and group_concat these keys
The contents of the main_info table
MariaDB [demos]> select * from main_info; +------+------+-------+-------+----------+ | key1 | key2 | info1 | info2 | date | +------+------+-------+-------+----------+ | 1 | 1 | 15 | 90 | 20120501 | | 1 | 2 | 14 | 92 | 20120601 | | 1 | 3 | 15 | 82 | 20120801 | | 1 | 4 | 15 | 82 | 20120801 | | 1 | 5 | 15 | 82 | 20120802 | | 2 | 1 | 17 | 90 | 20130302 | | 2 | 2 | 17 | 90 | 20130302 | | 2 | 3 | 17 | 90 | 20130302 | | 2 | 4 | 16 | 88 | 20130601 | +------+------+-------+-------+----------+ 9 rows in set (0.00 sec)
The contents of the table product1
MariaDB [demos]> select * from product1; +------+------+--------+--------------+ | key1 | key2 | serial | product_data | +------+------+--------+--------------+ | 1 | 1 | 0 | NaN | | 1 | 1 | 1 | NaN | | 1 | 1 | 2 | NaN | | 1 | 1 | 3 | NaN | | 1 | 2 | 0 | 12.556 | | 1 | 2 | 1 | 13.335 | | 1 | 3 | 1 | NaN | | 1 | 3 | 2 | 13.556 | | 1 | 3 | 3 | 14.556 | | 1 | 4 | 3 | NaN | | 1 | 5 | 3 | NaN | | 2 | 1 | 0 | 12.556 | | 2 | 1 | 1 | 13.553 | | 2 | 1 | 2 | NaN | | 2 | 2 | 12 | 129 | | 2 | 3 | 22 | NaN | +------+------+--------+--------------+ 16 rows in set (0.00 sec)
Through PHP, I group the info1 and info2 fields of the info1 table, in the current context serial , product_data the product1 table, several times one after another (here I run the query twice, as you can see)
For serial field - 1st request
MariaDB [demos]> select * , count(*) as serial_count,GROUP_CONCAT(key1,' ',key2) as serial_ids from -> ( -> SELECT distinct -> if(b.serial < 10,a.key1,null) AS `key1`, -> if(b.serial < 10,a.key2,null) AS `key2`, -> if(b.serial < 10,a.info1,null) AS `info1`, -> if(b.serial < 10,a.info2,null) AS `info2` -> FROM main_info a inner join product1 b on a.key1 = b.key1 AND a.key2= b.key2 -> ) as sub group by info1,info2 -> ; +------+------+-------+-------+--------------+-------------+ | key1 | key2 | info1 | info2 | serial_count | serial_ids | +------+------+-------+-------+--------------+-------------+ | NULL | NULL | NULL | NULL | 1 | NULL | | 1 | 2 | 14 | 92 | 1 | 1 2 | | 1 | 3 | 15 | 82 | 3 | 1 3,1 4,1 5 | | 1 | 1 | 15 | 90 | 1 | 1 1 | | 2 | 1 | 17 | 90 | 1 | 2 1 | +------+------+-------+-------+--------------+-------------+ 5 rows in set (0.00 sec)
For product_data field - 2nd request
MariaDB [demos]> select * , count(*) as product_data_count,GROUP_CONCAT(key1,' ',key2) as product_data_ids from -> ( -> SELECT distinct -> if(b.product_data IS NOT NULL,a.key1,null) AS `key1`, -> if(b.product_data IS NOT NULL,a.key2,null) AS `key2`, -> if(b.product_data IS NOT NULL,a.info1,null) AS `info1`, -> if(b.product_data IS NOT NULL,a.info2,null) AS `info2` -> FROM main_info a inner join product1 b on a.key1 = b.key1 AND a.key2= b.key2 -> ) as sub group by info1,info2 -> ; +------+------+-------+-------+--------------------+------------------+ | key1 | key2 | info1 | info2 | product_data_count | product_data_ids | +------+------+-------+-------+--------------------+------------------+ | 1 | 2 | 14 | 92 | 1 | 1 2 | | 1 | 3 | 15 | 82 | 3 | 1 3,1 4,1 5 | | 1 | 1 | 15 | 90 | 1 | 1 1 | | 2 | 2 | 17 | 90 | 3 | 2 2,2 3,2 1 | +------+------+-------+-------+--------------------+------------------+ 4 rows in set (0.01 sec)
I would like to get this conclusion using one query: Group by info1, info2
+------+------+-------+-------+--------------+-------------+--------------------+------------------+ | key1 | key2 | info1 | info2 | serial_count | serial_ids | product_data_count | product_data_ids | +------+------+-------+-------+--------------+-------------+--------------------+------------------+ | NULL | NULL | NULL | NULL | 1 | NULL | NULL | NULL | | 1 | 2 | 14 | 92 | 1 | 1 2 | 1 | 1 2 | | 1 | 3 | 15 | 82 | 3 | 1 3,1 4,1 5 | 3 | 1 3,1 4,1 5 | | 1 | 1 | 15 | 90 | 1 | 1 1 | 1 | 1 1 | | 2 | 1 | 17 | 90 | 1 | 2 1 | 3 | 2 2,2 3,2 1 | +------+------+-------+-------+--------------+-------------+--------------------+------------------+
The table structure is shown below.
DROP TABLE IF EXISTS `main_info`; CREATE TABLE `main_info` ( `key1` int(11) NOT NULL, `key2` int(11) NOT NULL, `info1` int(11) NOT NULL, `info2` int(11) NOT NULL, `date` int(11) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; LOCK TABLES `main_info` WRITE; INSERT INTO `main_info` VALUES (1,1,15,90,20120501),(1,2,14,92,20120601),(1,3,15,82,20120801),(1,4,15,82,20120801),(1,5,15,82,20120802),(2,1,17,90,20130302),(2,2,17,90,20130302),(2,3,17,90,20130302),(2,4,16,88,20130601); UNLOCK TABLES; DROP TABLE IF EXISTS `product1`; CREATE TABLE `product1` ( `key1` int(11) NOT NULL, `key2` int(11) NOT NULL, `serial` int(11) NOT NULL, `product_data` varchar(1000) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; LOCK TABLES `product1` WRITE; INSERT INTO `product1` VALUES (1,1,0,'NaN'),(1,1,1,'NaN'),(1,1,2,'NaN'),(1,1,3,'NaN'),(1,2,0,'12.556'),(1,2,1,'13.335'),(1,3,1,'NaN'),(1,3,2,'13.556'),(1,3,3,'14.556'),(1,4,3,'NaN'),(1,5,3,'NaN'),(2,1,0,'12.556'),(2,1,1,'13.553'),(2,1,2,'NaN'),(2,2,12,'129'),(2,3,22,'NaN'); UNLOCK TABLES;
Someone please help me get the result in one query.