Issue Details (XML | Word | Printable)

Key: CORE-5435
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Dmitry Yemanov
Reporter: Paquito Ines
Votes: 0
Watchers: 7
Operations

If you were logged in you would be able to see more operations.
Firebird Core

Badly selective index could be used for extra filtering even if selective index is used for sorting

Created: 04/Jan/17 07:30 PM   Updated: 22/Feb/17 02:49 PM
Component/s: Engine
Affects Version/s: 4.0 Initial, 3.0.0, 3.0.1
Fix Version/s: 3.0.2, 4.0 Alpha 1

File Attachments: 1. Zip Archive Databases.zip (5.75 MB)


QA Status: Done successfully


 Description  « Hide
It seems that Firebird 3 is sometimes choosing the index with less selectivity, which can have a serious
effect on performance. We have seen this behaviour in several queries.

In this example (we have attached in databases.zip the databases for Firebird 2.5 and Firebird 3):
  -Table1 has around 300000 records.
  -Field1 has 15000 different values with 20 records each one.
  -Field2 has 2 different values (0 with 200000 records, and 1 with 100000 records).
  -Both databases were tested immediately after a backup/restore cycle and in the same computer.
  -The times are measured in the second execution of each query (though the first execution follow the same pattern).

Query:
  select *
  from TABLE1 where FIELD1 is null and FIELD2=0
  order by FIELD1, ID

Firebird 2.1/2.5:
  PLAN (TABLE1 ORDER TABLE1_F1_ID INDEX (TABLE1_F1_ID))
  0.002 seconds

Firebird3:
  PLAN (TABLE1 ORDER TABLE1_F1_ID INDEX (TABLE1_F2))
  Select Expression
      -> Filter
          -> Table "TABLE1" Access By ID
              -> Index "TABLE1_F1_ID" Range Scan (partial match: 1/2)
                  -> Bitmap
                      -> Index "TABLE1_F2" Range Scan (full match)
  0.240 seconds

-

CREATE TABLE "TABLE1"
(
  "ID" INTEGER NOT NULL,
  "FIELD1" INTEGER,
  "FIELD2" INTEGER,
CONSTRAINT "TABLE1_PK" PRIMARY KEY ("ID")
);

CREATE INDEX "TABLE1_F1_ID" ON "TABLE1"("FIELD1", "ID");
CREATE INDEX "TABLE1_F2" ON "TABLE1"("FIELD2");


 All   Comments   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Paquito Ines added a comment - 12/Jan/17 11:03 AM
We have tested the issue with the Nightly Build, and, though the plan has changed and no longer uses the low selectivity index, the performance has not improved and is even slightly worse.

Firebird-3.0.2.32664-0_x64 (nightly build):
  PLAN (TABLE1 ORDER TABLE1_F1_ID)
  Select Expression
      -> Filter
          -> Table "TABLE1" Access By ID
              -> Index "TABLE1_F1_ID" Range Scan (partial match: 1/2)
  0.290 seconds

Maybe is using TABLE1_F1_ID only for ordered retrieval and not for filtering?

Forcing the Firebird 2.5 plan works well:
  select *
  from TABLE1 where FIELD1 is null and FIELD2=0
  PLAN (TABLE1 ORDER TABLE1_F1_ID INDEX (TABLE1_F1_ID))
  order by FIELD1, ID

  PLAN (TABLE1 ORDER TABLE1_F1_ID INDEX (TABLE1_F1_ID))
  Select Expression
      -> Filter
          -> Table "TABLE1" Access By ID
              -> Index "TABLE1_F1_ID" Range Scan (partial match: 1/2)
                  -> Bitmap
                      -> Index "TABLE1_F1_ID" Range Scan (partial match: 1/2)
  0.002 seconds

Paquito Ines added a comment - 13/Jan/17 12:23 PM
Further testing with Firebird 3.0.2.nightly shows a possible problem with the "FIELD1 is null" condition, as it seems to not be using the index for filtering it, but it is working well if the condition is "FIELD1=0", tough both queries have the same plan.

Firebird-3.0.2.32664-0_x64 (nightly build):
  select *
  from TABLE1 where FIELD1 is null and FIELD2=0
  order by FIELD1, ID

  PLAN (TABLE1 ORDER TABLE1_F1_ID)
  Select Expression
      -> Filter
          -> Table "TABLE1" Access By ID
              -> Index "TABLE1_F1_ID" Range Scan (partial match: 1/2)
  0.290 seconds

Firebird-3.0.2.32664-0_x64 (nightly build):
  select *
  from TABLE1 where FIELD1=0 and FIELD2=0
  order by FIELD1, ID;

  PLAN (TABLE1 ORDER TABLE1_F1_ID)
  Select Expression
      -> Filter
          -> Table "TABLE1" Access By ID
              -> Index "TABLE1_F1_ID" Range Scan (partial match: 1/2)
  0.002 s

However, in Firebird 2.5, both "FIELD1 is null" and "FIELD1=0" queries have same plan and same timing:
  PLAN (TABLE1 ORDER TABLE1_F1_ID INDEX (TABLE1_F1_ID))
  0.002 s

Dmitry Yemanov added a comment - 13/Jan/17 05:20 PM
It appears to be a completely unrelated bug, related to NULLs filtering inside the navigational index scan. I'm investigating.

Paquito Ines added a comment - 22/Feb/17 11:14 AM
Do you want that we submit a new report for this new bug, so it will be easily tracked?

Dmitry Yemanov added a comment - 22/Feb/17 02:49 PM
Please create one. I already have a fix, just need to double check.