Answer the question
In order to leave comments, you need to log in
How to index fields in the database?
Hello.
I decided to practice and solve the problem. Given a table with a tree of categories:
CREATE TABLE category (
id integer not null primary key,
parent_category_id integer references category(id),
name varchar(100) not null
);
SELECT id, parent_category_id, name FROM category WHERE parent_category_id IS NULL AND name LIKE 'авто%';
SELECT id, name, parent_category_id FROM test_270220.category WHERE parent_category_id IN
(SELECT parent_category_id
FROM test_270220.category
GROUP BY parent_category_id
HAVING COUNT(parent_category_id) < 4
);
SELECT cat1.id, cat1.name, cat1.parent_category_id
FROM test_270220.category AS cat1
LEFT JOIN test_270220.category AS cat2 ON cat2.parent_category_id = cat1.id
WHERE cat2.id IS NULL;
Write indexes that will make these queries faster.
CREATE FULLTEXT INDEX name on test_270220.category(name)
CREATE INDEX idx_category_parent_category_id ON test_270220.category (parent_category_id)
Answer the question
In order to leave comments, you need to log in
I don’t interact with SQL so often, googling I found the following options:
mmm. well here it is necessary to rewrite 2 requests and to add 2 indexes and to suppress in one request on one index. so it will be right.
But after creating the indexes, the query time increased
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question