MySQLで文字列が空文字でない行を調べる場合、どうやって最適化すればいいのでしょうか。
インデックスを割り振っても否定形「列!=''」になるため速度が出ません。
否定形を使わない形「(列='')=false」にすると3分かかっていたのは2分半になるぐらいには早くなりますが、もっと速度が必要です。
空文字かどうかを記録する列を追加するぐらいしか思いつきませんが、空文字を調べたい列の数だけ列を追加するのはできればやりたくありません。
何か高速化する方法はあればご教示いただけないでしょうか。
バージョン:8.0.18
クエリ
//否定形のクエリ select * from asin where JAN !='' limit 251063,1; //否定形をつかわないクエリ select * from asin where (JAN ='')=false limit 251063,1;
###create table
| asin | CREATE TABLE `asin` ( `ASIN` varchar(10) COLLATE utf8mb4_general_ci NOT NULL, `JAN` varchar(13) COLLATE utf8mb4_general_ci NOT NULL, `Title` varchar(1024) COLLATE utf8mb4_general_ci DEFAULT NULL, `ImageUrl` varchar(2048) COLLATE utf8mb4_general_ci DEFAULT NULL, `FixFee` smallint(6) DEFAULT NULL, `FeeRate` float(2,2) DEFAULT NULL, `PriceFBA` int(11) DEFAULT NULL, `PriceSelf` int(11) DEFAULT NULL, `PriceCart` int(11) DEFAULT NULL, `NotExit` bit(1) NOT NULL, `SettingFee` bit(2) NOT NULL, `SettingProductItem` bit(2) NOT NULL, `SettingJAN` bit(2) NOT NULL, `PriceSetTime` bigint(20) NOT NULL, `ProductItemSetTime` bigint(20) NOT NULL, `FeeSetTime` bigint(20) NOT NULL, `SellerNum` smallint(6) DEFAULT NULL, `HadFBA` bit(1) NOT NULL, `SettingMonorate` bit(2) NOT NULL, `MonorateSetTime` bigint(20) NOT NULL, `MonorateBuyNum` smallint(6) NOT NULL, `TitleOpt` varchar(1024) COLLATE utf8mb4_general_ci DEFAULT NULL, `Danger` bit(1) NOT NULL, `Claim` bit(1) NOT NULL, `MonorateNoExitRate` float(3,3) NOT NULL, `MonorateNoExitRateTotal` float(3,3) NOT NULL, `SettingCategory` bit(2) NOT NULL, `CategorySetTime` bigint(20) NOT NULL, `CategoryHash` int(10) unsigned DEFAULT NULL, `CategoryHash2` int(10) unsigned DEFAULT NULL, `CategoryHash3` int(10) unsigned DEFAULT NULL, `PriceUsedFBA` int(11) DEFAULT NULL, `PriceUsedSelf` int(11) DEFAULT NULL, `DHashSetTime` bigint(20) NOT NULL, `DHash` varchar(1024) COLLATE utf8mb4_general_ci DEFAULT NULL, `RankingCategoryHash1` int(10) unsigned DEFAULT NULL, `RankingCategoryHash2` int(10) unsigned DEFAULT NULL, `RankingCategoryHash3` int(10) unsigned DEFAULT NULL, `Ranking1` int(11) DEFAULT NULL, `Ranking2` int(11) DEFAULT NULL, `Ranking3` int(11) DEFAULT NULL, `PricePreFBA` int(11) DEFAULT NULL, `PricePreSelf` int(11) DEFAULT NULL, `PricePreUsedFBA` int(11) DEFAULT NULL, `PricePreUsedSelf` int(11) DEFAULT NULL, `PricePreCart` int(11) DEFAULT NULL, `SetTypeSer` smallint(5) unsigned DEFAULT NULL, `NoCommunicationTick` bigint(20) NOT NULL, `Width` int(10) unsigned DEFAULT NULL, `Height` int(10) unsigned DEFAULT NULL, `Lenght` int(10) unsigned DEFAULT NULL, `Weight` int(10) unsigned DEFAULT NULL, `SizeTypeSer` smallint(5) unsigned NOT NULL, `JAN2` varchar(13) COLLATE utf8mb4_general_ci NOT NULL, `JAN3` varchar(13) COLLATE utf8mb4_general_ci NOT NULL, `JAN4` varchar(13) COLLATE utf8mb4_general_ci NOT NULL, `JAN5` varchar(13) COLLATE utf8mb4_general_ci NOT NULL, `Model` varchar(64) COLLATE utf8mb4_general_ci DEFAULT NULL, PRIMARY KEY (`ASIN`), KEY `noc_idx` (`NoCommunicationTick`), KEY `price_idx` (`PriceSetTime`), KEY `product_idx` (`ProductItemSetTime`), KEY `dhash_idx` (`DHashSetTime`), KEY `monoratetime_idx` (`MonorateSetTime`), KEY `rank_idx` (`Ranking1`), KEY `monorate_idx` (`MonorateBuyNum`), KEY `fee_monorate_idx` (`FeeSetTime`,`MonorateBuyNum`), KEY `category_idx` (`CategorySetTime`), KEY `ureteru_get_index` (`MonorateBuyNum`,`SettingProductItem`,`NotExit`,`ASIN`), KEY `jansindex` (`JAN`), KEY `jan2index` (`JAN2`), KEY `jan3index` (`JAN3`), KEY `jan4index` (`JAN4`), KEY `jan5index` (`JAN5`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci |
###explain
//否定形 mysql> explain -> select * from asin -> where JAN !='' -> limit 251063,1; +----+-------------+-------+------------+-------+---------------+-----------+---------+------+--------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+-----------+---------+------+--------+----------+-----------------------+ | 1 | SIMPLE | asin | NULL | range | jansindex | jansindex | 54 | NULL | 595973 | 100.00 | Using index condition | +----+-------------+-------+------------+-------+---------------+-----------+---------+------+--------+----------+-----------------------+ //否定形なし mysql> explain select * from asin -> where (JAN ='')=false -> limit 251063,1; +----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------------+ | 1 | SIMPLE | asin | NULL | ALL | NULL | NULL | NULL | NULL | 6890143 | 100.00 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------------+