How does the btree index work?
Posted by:
Niki Mas
Date: February 02, 2024 07:49AM
Hi all! Please, help me solve the problem. The categorization table has two columns with a range of values - one column contains the minimum of the range, the second contains the maximum of the range. The table configuration looks something like this:
+--+----------+-----------+-----------+
| id | _min | _max | categ_id |
+--+----------+-----------+-----------+
| 1 | 100 | 200 | 2 |
| 2 | 201 | 300 | 5 |
| 3 | 301 | 400 | 8 |
+--+----------+-----------+-----------+
There are several thousand records in the table.
There is a composite UNIQUE index on the fields (_min, _max)
I need to find the range where the desired value fits.
SELECT categ_id FROM tcat WHERE 205 BETWEEN _min AND _max
SELECT categ_id FROM tcat WHERE 205 _min <= 205 AND _max >= 205
Now, when I look for a value at the beginning of the list (conditionally 150), the index works and the query processes quickly.
When I search for a value in the middle of the list (relatively 250), the index no longer works and the query is slower.
And when I look for a value from the end of the list (conditionally 350), the index does not work and the query processes even more slowly.
I tried to make another composite index, rearranging the columns (_max, _min), the situation was reversed - the fastest search was from the end of the list, the search from the beginning of the list turned out to be the slowest. Searching from the middle of the list remained just as slow.
I haven't worked much with MySQL yet and I can't understand this behavior. The SQL Server did not observe this.
It seems that the index is not tree-like, but just a regular sorted list.
Could anyone tell me why the index behaves this way and how to create the index correctly so that it is used for values from the middle of the list? After all, the selectivity is very high.
Thanks in advance!
Subject
Written By
Posted
How does the btree index work?
February 02, 2024 07:49AM
Sorry, only registered users may post in this forum.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.