Skip to main content
added 457 characters in body
Source Link
sdeav
  • 3
  • 1
  • 5

I have a very simple table with 100,000 rows.

CREATE TABLE test_database.products (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `city_id` int  NOT NULL,
  `market_id` int NOT NULL,
  `product_id` int NOT NULL,
  `search` varchar(450) COLLATE utf8mb4_unicode_ci NOT NULL,
  `quantity` int NOT NULL,
  `price` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL,
   PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8
COLLATE=utf8_general_ci;

I added fulltext:

ALTER TABLE products ADD fulltext (search);

And when I search:

    SELECT 
        id, market_id, product_id, quantity, price,
        MATCH(pr.search) AGAINST('dolor* qui*' IN BOOLEAN MODE) as srv 
    from 
        products pr 
    where 
        pr.city_id = 1 and
        MATCH(pr.search) AGAINST('dolor* qui*' IN BOOLEAN MODE) 
    ORDER by
        srv DESC;

It takes ~ 2500 ms.

Explain select ...

id|select_type|table|partitions|type    |possible_keys|key   |key_len|ref  |rows|filtered|Extra                        |
--|-----------|-----|----------|--------|-------------|------|-------|-----|----|--------|-----------------------------|
 1|SIMPLE     |pr   |          |fulltext|search       |search|0      |const|   1|    10.0|Using where; Ft_hints: sorted|

MySQL version:

 Ver 8.0.21 for Win64 on x86_64

What I'm doing wrong?

I have a very simple table with 100,000 rows.

CREATE TABLE test_database.products (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `city_id` int  NOT NULL,
  `market_id` int NOT NULL,
  `product_id` int NOT NULL,
  `search` varchar(450) COLLATE utf8mb4_unicode_ci NOT NULL,
  `quantity` int NOT NULL,
  `price` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL,
   PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8
COLLATE=utf8_general_ci;

I added fulltext:

ALTER TABLE products ADD fulltext (search);

And when I search:

    SELECT 
        id, market_id, product_id, quantity, price,
        MATCH(pr.search) AGAINST('dolor* qui*' IN BOOLEAN MODE) as srv 
    from 
        products pr 
    where 
        pr.city_id = 1 and
        MATCH(pr.search) AGAINST('dolor* qui*' IN BOOLEAN MODE) 
    ORDER by
        srv DESC;

It takes ~ 2500 ms.

What I'm doing wrong?

I have a very simple table with 100,000 rows.

CREATE TABLE test_database.products (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `city_id` int  NOT NULL,
  `market_id` int NOT NULL,
  `product_id` int NOT NULL,
  `search` varchar(450) COLLATE utf8mb4_unicode_ci NOT NULL,
  `quantity` int NOT NULL,
  `price` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL,
   PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8
COLLATE=utf8_general_ci;

I added fulltext:

ALTER TABLE products ADD fulltext (search);

And when I search:

    SELECT 
        id, market_id, product_id, quantity, price,
        MATCH(pr.search) AGAINST('dolor* qui*' IN BOOLEAN MODE) as srv 
    from 
        products pr 
    where 
        pr.city_id = 1 and
        MATCH(pr.search) AGAINST('dolor* qui*' IN BOOLEAN MODE) 
    ORDER by
        srv DESC;

It takes ~ 2500 ms.

Explain select ...

id|select_type|table|partitions|type    |possible_keys|key   |key_len|ref  |rows|filtered|Extra                        |
--|-----------|-----|----------|--------|-------------|------|-------|-----|----|--------|-----------------------------|
 1|SIMPLE     |pr   |          |fulltext|search       |search|0      |const|   1|    10.0|Using where; Ft_hints: sorted|

MySQL version:

 Ver 8.0.21 for Win64 on x86_64

What I'm doing wrong?

Source Link
sdeav
  • 3
  • 1
  • 5

MySQL fulltext query is very slow | What I'm doing wrong?

I have a very simple table with 100,000 rows.

CREATE TABLE test_database.products (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `city_id` int  NOT NULL,
  `market_id` int NOT NULL,
  `product_id` int NOT NULL,
  `search` varchar(450) COLLATE utf8mb4_unicode_ci NOT NULL,
  `quantity` int NOT NULL,
  `price` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL,
   PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8
COLLATE=utf8_general_ci;

I added fulltext:

ALTER TABLE products ADD fulltext (search);

And when I search:

    SELECT 
        id, market_id, product_id, quantity, price,
        MATCH(pr.search) AGAINST('dolor* qui*' IN BOOLEAN MODE) as srv 
    from 
        products pr 
    where 
        pr.city_id = 1 and
        MATCH(pr.search) AGAINST('dolor* qui*' IN BOOLEAN MODE) 
    ORDER by
        srv DESC;

It takes ~ 2500 ms.

What I'm doing wrong?