English 中文(简体)
MySql 8复合指数,加上指数化表述“不作废”
原标题:MySql 8 composite index with the indexed expression "is not null" not being used

I m 采用Mysql 8.0.33。

我创建了一个包含以下表述的复合指数:column_name is not void,并且无法使用该指数。

我做了什么错误?

CREATE TABLE appointments 
(
  id INT AUTO_INCREMENT PRIMARY KEY,
  user_id INT NOT NULL,
  recurrence_rule VARCHAR(255)
);

CREATE INDEX idx1
ON appointments ((recurrence_rule IS NOT NULL), user_id);

explain analyze select * from appointments where user_id = 7 and recurrence_rule is not null;

-> Filter:(aappointments.user_id = 7) and (appointments.recurrence_rule is not void) (cost=150.25 rows=135) (actual time=0.356.0.625 rows=4 loops=1) -> Table scan on accreditation (cost=150.25 rows=1500) (actual time=0.143.0.528 rows=1500 loops=1)

Details on use case

我有一个数据库表,包含数百万人的任命。 这些任命中有一些重复(其中只有1%)并按重复模式加以界定。

我要高效地询问属于用户和重复任用的所有任用。 用户可能拥有10 000个任用,其中100个是重复任用。

为此,我制定了关于以下表述的综合指数:<代码>recurrence_rule is notrn和user_id。 但这一指数并未使用。 尤其是,似乎我可以拿到<代码>recurrence_rule is notrn,以便与指数一致。

Generate sample data

DELIMITER $$

CREATE PROCEDURE GenerateData()
BEGIN
    -- Declare variables
    DECLARE i INT DEFAULT 1; -- Loop counter
    DECLARE user_id INT;
    DECLARE recurrence_rule VARCHAR(255);
    
    -- every 25 appointments is a recurring appointment
    -- each user gets 100 appointments
    WHILE i <= 1500 DO
        SET user_id = CEIL(i / 100.0); -- Calculate user_id, ensure it s rounded up
        
        -- Check if the appointment is recurring
        IF i % 25 = 0 THEN
            SET recurrence_rule = CONCAT( FREQ= , ELT(FLOOR(1 + RAND() * 4),  DAILY ,  WEEKLY ,  MONTHLY ,  YEARLY ),  ;INTERVAL= , FLOOR(1 + RAND() * 10));
        ELSE
            -- Set recurrence_rule to NULL for non-recurring appointments
            SET recurrence_rule = NULL;
        END IF;
        
        -- Insert data into appointments table
        INSERT INTO appointments (user_id, recurrence_rule)
        VALUES (user_id, recurrence_rule);
        
        -- Increment the loop counter
        SET i = i + 1;
    END WHILE;
    
END$$

DELIMITER ;
call GenerateData;
最佳回答
问题回答

我认为(无证据)这更好的指数:

INDEX(user_id, recurrence_rule)  -- idx3

<<<><>>t>>: 1, “cost_info”:{“query_cost”:“1.15”},“table_name”:“appointments”,“access_type”:“range”,“possible_keys”: [idx3”,“idx2”,“key”:“edx3”,“use_key_parts”: [user_id, “recurrence_”

说明“用钥匙_parts”、“key_length”和“rows_Reviewd”

你也可以尝试(但似乎并不好):

INDEX(user_id, (recurrence_rule IS NOT NULL))




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签