Я пытаюсь выполнить запрос, но слишком много соединений, следовательно, медленное время выполнения. Я смотрю на другие альтернативы, такие как PHP вместо mysql. Все еще ломаю голову над тем, сколько запросов / соединений мне нужно запустить. Любые предложения?
5 Таблицы и их ряды
2Вариант увеличивается на 5000 строк в год
ди увеличивается, может быть, на 100 000 в год
документ увеличивается на 50 000 в год
type и hashtag являются константами около 20
VARIABLES
Теперь вот что с моим запросом
$ groups = group by type ИЛИ group by hashtag ИЛИ оба
$ query_where = REGEXP, чтобы найти только определенный тип и / или хэштег из списка массива (выбранного пользователем), например, find_in_set
$query_where .= "AND CONCAT(',', `type.product_type`, ',') REGEXP ',($types_arr),' ";
Запрос ниже выполняется около 38 секунд
Это только в том случае, если $ groups = type.product_type only
и
$ query_where = ""
SELECT
type.product_type as type, hashtag.variation_hashtag as hashtag,
di.warehouse, sum(di.qty)as qty, sum(di.price) as price, sum(di.sold_price) as sold_price, sum(di.cogs) as cogs, avg(di.price) as ave_price, avg(di.sold_price) as ave_sold_price,
YEAR(docs.created_on) as year, MONTH(docs.created_on) as month
FROM 2Variations v
LEFT JOIN docs_inventory as di on di.product_id = v.product_id
LEFT JOIN docs on docs.id = di.doc_id
LEFT JOIN variations_type_link as type on v.id = type.id
LEFT JOIN variations_hashtag_link as hashtag on v.id = hashtag.id
WHERE
di.deleted = 0 and
type.deleted = 0 and
(hashtag.deleted = 0 or hashtag.deleted is null) and
di.warehouse between 1 and 2
".$query_where."
GROUP BY
YEAR(docs.created_on), MONTH(docs.created_on), ".$groups;
EXPLAIN
+----+-------------+---------+--------+--------------------+------------+---------+-----------------------+------+----------------------------------------------+--+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | |
+----+-------------+---------+--------+--------------------+------------+---------+-----------------------+------+----------------------------------------------+--+
| 1 | SIMPLE | type | ALL | PRIMARY,deleted | NULL | NULL | NULL | 4730 | Using where; Using temporary; Using filesort | |
| 1 | SIMPLE | v | eq_ref | PRIMARY,product_id | PRIMARY | 4 | database.type.id | 1 | Using where | |
| 1 | SIMPLE | hashtag | eq_ref | PRIMARY | PRIMARY | 4 | database.v.id | 1 | Using where | |
| 1 | SIMPLE | di | ref | product_id | product_id | 4 | database.v.product_id | 218 | Using where | |
| 1 | SIMPLE | docs | eq_ref | PRIMARY | PRIMARY | 4 | database.di.doc_id | 1 | NULL | |
+----+-------------+---------+--------+--------------------+------------+---------+-----------------------+------+----------------------------------------------+--+
ТАБЛИЦА СОЗДАНИЯ
Create Table
2Variations CREATE TABLE `2Variations` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`customer_id` int(11) NOT NULL,
`auto_reorder` tinyint(1) NOT NULL DEFAULT '0',
`product_id` int(11) NOT NULL,
`qty_name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`color` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`supplier_code` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`barcode` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`upca` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`no_pic` varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`restock` int(11) NOT NULL DEFAULT '1',
`hashtag` text COLLATE utf8mb4_unicode_ci,
`php_wholesale` decimal(11,2) DEFAULT NULL,
`php_retail` decimal(11,2) DEFAULT NULL,
`weight` decimal(11,2) NOT NULL DEFAULT '0.00',
`notes` text COLLATE utf8mb4_unicode_ci,
`company_notes` text COLLATE utf8mb4_unicode_ci,
`mgnt_notes` text COLLATE utf8mb4_unicode_ci,
`deleted` int(11) NOT NULL DEFAULT '0',
`retired` tinyint(4) NOT NULL DEFAULT '0',
`created_by` int(20) NOT NULL,
`created_on` datetime NOT NULL,
`updated_by` int(20) NOT NULL,
`last_update` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `product_id` (`product_id`),
KEY `customer_id` (`customer_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5083 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
docs CREATE TABLE `docs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`customer_id` int(11) NOT NULL COMMENT 'id in table customers',
`registered_id` int(20) NOT NULL DEFAULT '0',
`doc_type` int(1) NOT NULL DEFAULT '0' COMMENT '0: none, 1:PO, 2:DR, 3:INV, 4: RS',
`doc_no` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
`doc_date` date NOT NULL,
`duedate` date DEFAULT NULL,
`confirm_date` date NOT NULL,
`paid` date NOT NULL,
`paid_amount` decimal(11,2) DEFAULT NULL,
`sf` decimal(11,2) DEFAULT NULL,
`rsf` decimal(11,2) DEFAULT NULL,
`vat` decimal(11,2) DEFAULT NULL,
`vatinex` int(1) DEFAULT NULL COMMENT '1 = in, 2 = ex',
`merc_total` decimal(11,2) NOT NULL DEFAULT '0.00',
`payment` decimal(11,2) NOT NULL DEFAULT '0.00',
`commission` decimal(11,2) NOT NULL DEFAULT '0.00',
`discount` decimal(11,2) DEFAULT '0.00',
`voucher_amount` decimal(11,2) NOT NULL DEFAULT '0.00',
`bundle_discount_amount` decimal(11,2) NOT NULL DEFAULT '0.00',
`shipped` datetime DEFAULT NULL,
`ext_courier` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`ext_courier_tracking` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`ext_orderid` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`ext_total_weight` decimal(11,4) NOT NULL,
`notes` longtext COLLATE utf8mb4_unicode_ci,
`company_notes` text COLLATE utf8mb4_unicode_ci,
`mgnt_notes` longtext COLLATE utf8mb4_unicode_ci,
`created_by` int(11) NOT NULL COMMENT 'id in users',
`created_on` datetime NOT NULL,
`updated_by` int(11) NOT NULL COMMENT 'id in users',
`last_update` datetime NOT NULL,
`deleted` int(11) NOT NULL DEFAULT '0',
`deleted_reason` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `doc_no` (`doc_no`),
KEY `customer_id` (`customer_id`),
KEY `ext_courier_tracking` (`ext_courier_tracking`,`ext_orderid`),
KEY `ext_orderid` (`ext_orderid`)
) ENGINE=InnoDB AUTO_INCREMENT=77904 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
docs_inventory CREATE TABLE `docs_inventory` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`doc_id` int(11) NOT NULL COMMENT 'id in table doc',
`product_id` int(11) NOT NULL COMMENT 'id in table products',
`color` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`qty` int(11) NOT NULL DEFAULT '0',
`warehouse` int(11) NOT NULL DEFAULT '1',
`price` decimal(11,2) NOT NULL DEFAULT '0.00',
`sold_price` decimal(11,2) NOT NULL DEFAULT '0.00',
`cogs` decimal(11,2) DEFAULT NULL,
`deleted` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `doc_id` (`doc_id`),
KEY `product_id` (`product_id`)
) ENGINE=InnoDB AUTO_INCREMENT=395438 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
products_type_link CREATE TABLE `products_type_link` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(20) NOT NULL,
`product_type` int(20) NOT NULL,
`created_by` int(20) NOT NULL,
`created_on` datetime NOT NULL,
`updated_by` int(20) NOT NULL,
`last_update` datetime NOT NULL,
`deleted` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `product_type` (`product_type`)
) ENGINE=InnoDB AUTO_INCREMENT=4088 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
variations_hashtag_link CREATE TABLE `variations_hashtag_link` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`variation_id` int(20) NOT NULL,
`variation_hashtag` int(20) NOT NULL,
`created_by` int(20) NOT NULL,
`created_on` datetime NOT NULL,
`updated_by` int(20) NOT NULL,
`last_update` datetime NOT NULL,
`deleted` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `variation_hashtag` (`variation_hashtag`)
) ENGINE=InnoDB AUTO_INCREMENT=3703 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
ОБНОВЛЕННЫЙ ИНДЕКС, чтобы включить Удаленный, стал медленнее до 45 сек
+----+-------------+---------+--------+------------------------------+------------+---------+------------------------+--------+----------------------------------------------+--+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | |
+----+-------------+---------+--------+------------------------------+------------+---------+------------------------+--------+----------------------------------------------+--+
| 1 | SIMPLE | di | ref | product_id,warehouse,deleted | deleted | 4 | const | 169837 | Using where; Using temporary; Using filesort | |
| 1 | SIMPLE | docs | eq_ref | PRIMARY | PRIMARY | 4 | database.di.doc_id | 1 | NULL | |
| 1 | SIMPLE | v | ref | PRIMARY,product_id | product_id | 4 | database.di.product_id | 2 | Using index | |
| 1 | SIMPLE | hashtag | eq_ref | PRIMARY | PRIMARY | 4 | database.v.id | 1 | Using where | |
| 1 | SIMPLE | type | eq_ref | PRIMARY,deleted,deleted_2 | PRIMARY | 4 | database.v.id | 1 | Using where | |
+----+-------------+---------+--------+------------------------------+------------+---------+------------------------+--------+----------------------------------------------+--+