简体   繁体   中英

Partitions by null values with MySQL

I have a table:

CREATE TABLE `NewTable` (
    `IBLOCK_ELEMENT_ID`  int(11) NOT NULL ,
    `PROPERTY_1836`  int(11) NULL DEFAULT NULL ,
    `DESCRIPTION_1836`  varchar(255) CHARACTER SET cp1251 COLLATE cp1251_general_ci NULL DEFAULT NULL ,
    `PROPERTY_1837`  int(11) NULL DEFAULT 0 ,
    `DESCRIPTION_1837`  varchar(255) CHARACTER SET cp1251 COLLATE cp1251_general_ci NULL DEFAULT NULL ,
    `PROPERTY_1838`  decimal(18,4) NULL DEFAULT NULL ,
    `DESCRIPTION_1838`  varchar(255) CHARACTER SET cp1251 COLLATE cp1251_general_ci NULL DEFAULT NULL ,
    `PROPERTY_3139`  int(11) NULL DEFAULT 0 ,
    `DESCRIPTION_3139`  varchar(255) CHARACTER SET cp1251 COLLATE cp1251_general_ci NULL DEFAULT NULL ,
    `PROPERTY_3173`  decimal(18,4) NULL DEFAULT NULL ,
    `DESCRIPTION_3173`  varchar(255) CHARACTER SET cp1251 COLLATE cp1251_general_ci NULL DEFAULT NULL ,
    PRIMARY KEY (`IBLOCK_ELEMENT_ID`),
    INDEX `ix_perf_b_iblock_element_pr_1` (`PROPERTY_1837`) USING BTREE ,
    INDEX `ix_perf_b_iblock_element_pr_2` (`PROPERTY_1836`) USING BTREE ,
    INDEX `ix_perf_b_iblock_element_pr_3` (`PROPERTY_3139`) USING BTREE 
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=cp1251 COLLATE=cp1251_general_ci
ROW_FORMAT=COMPACT;

And a query with a condition:

WHERE PROPERTY_3139 IS NULL

I can't change a table or a query. But I know, if I'll split a table into 2 partitions - queries selecting only nullable values will work much faster.

What kind of trick can I use to do this? NULL and NOT NULL is not a range and I can't use it as a list of values.

PARTITION probably won't help.

NULL is a separate value in an INDEX. Think of NULLs as being stored in the INDEX before all the other values. Hence, IS NULL and IS NOT NULL can be treated as a 'range' for optimization purposes.

However... If more than 20% (10%-30%, depending on the phase of the moon) of a table is in the desired range, the optimizer will decide that it is faster to do a full table scan instead of bouncing back and forth between the INDEX and the Data.

Back to my probably ...

  • If a small number of rows have NULL, the index will do just fine; PARTITIONing won't help much.
  • If a medium number of rows have NULL, then PARTITIONing could help noticeably.
  • If most rows have NULL, the a full table scan is almost as good as scanning all of one PARTITION.

Note: You cannot PARTITION on more than one column. So, if you PARTITION on PROPERTY_3139 , the rest of the properties will be out of luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM