[MDEV-13704] Nested query does not give same result as subquery and an outer query Created: 2017-09-01  Updated: 2020-07-08  Resolved: 2020-07-08

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.2.6, 10.2.7, 10.2.8, 10.3.1
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: Jose Costa Teixeira Assignee: Varun Gupta (Inactive)
Resolution: Fixed Votes: 0
Labels: innodb
Environment:

Windows 64


Attachments: Text File maria_extra_settings.cfg.txt    
Issue Links:
Duplicate
is duplicated by MDEV-13994 Bad join results with orderby_uses_eq... Closed
Relates
relates to MDEV-13390 Identity server Db Select Statement o... Closed
relates to MDEV-13694 Wrong result upon GROUP BY with order... Closed

 Description   

A nested query does not return correct results in at least one case.

Scenario:

  • A nested query query (Select col... where col in (subquery)) returns empty.
  • Subquery correctly returns the rows (x,y,z).
  • OuterQuery takes (z,y,z) in an IN statement and correctly selects the relevant results (Select col... where col in (x,y,z))

This is not happening in 10.1 but only on 102 onwards.

I could not reproduce in a simpler scenario. Perhaps this is due to using fulltext search in the subquery, or any other reason.

Details:
https://stackoverflow.com/questions/45964454/subqueries-do-not-work-in-windows-but-works-on-linux

Tables are innodb. Attached the cfg settings I added to my.ini.



 Comments   
Comment by Jose Costa Teixeira [ 2017-09-05 ]

Update: After checking the result of EXPLAIN, it seems that this is related to the use of temporary tables (please forgive my language, I am not very familiar with SQL)
The version where it does not work has "Using filesort" while the one that works has a "Using temporary; Using filesort"
After finding that the temporary tables could be due to ORDER BY on a same column, I made a workaround :
added a "ORDER BY table1.entityKey DESC" in the subquery.
This seems to resolve the issue. Perhaps it points to a solution.

The query that works is (in bold underlined the addition to make it work) is:

Select table1.entityKey
from table1
where table1.Deleted = 0
and table1.MasterKey is null
and table1.entityTypeKey = 8
and table1.entityKey in
(select table2.entityKey
from table2
where table2.Flag <> 2
and (table2.IndexKey = 4 and MATCH (table2.xhtmltext) AGAINST ('gold')) ORDER BY entityKey DESC)
order by table1.entityKey DESC

Comment by Elena Stepanova [ 2017-09-19 ]

Thanks for the report. As a workaround, you can set optimizer_switch=orderby_uses_equalities=off in the config file, it should help.

Test case

--source include/have_innodb.inc
 
CREATE TABLE t1 (entityKey INT PRIMARY KEY) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1),(2),(3);
 
CREATE TABLE t2 (entityKey INT, xhtmltext TEXT, FULLTEXT(xhtmltext)) ENGINE=InnoDB;
INSERT INTO t2 VALUES (1,"silver"),(2,"gold"),(3,"gold"),(4,"gold");
 
SELECT t1.entityKey FROM t1 
WHERE t1.entityKey IN (
  SELECT t2.entityKey FROM t2 
  WHERE MATCH (t2.xhtmltext) AGAINST ('gold')
) 
ORDER BY t1.entityKey DESC;
 
# Cleanup
DROP TABLE t1, t2;

Actual result on 10.2 with default optimizer_switch

MariaDB [test]> SELECT t1.entityKey FROM t1 
    -> WHERE t1.entityKey IN (
    ->   SELECT t2.entityKey FROM t2 
    ->   WHERE MATCH (t2.xhtmltext) AGAINST ('gold')
    -> ) 
    -> ORDER BY t1.entityKey DESC;
Empty set (0.06 sec)

Expected result

MariaDB [test]> SELECT t1.entityKey FROM t1 
    -> WHERE t1.entityKey IN (
    ->   SELECT t2.entityKey FROM t2 
    ->   WHERE MATCH (t2.xhtmltext) AGAINST ('gold')
    -> ) 
    -> ORDER BY t1.entityKey DESC;
+-----------+
| entityKey |
+-----------+
|         3 |
|         2 |
+-----------+
2 rows in set (0.08 sec)

After fixing, please also check this test case. It differs from the first one only in the highlighted line, one extra value is inserted:

Another test case

--source include/have_innodb.inc
 
CREATE TABLE t1 (entityKey INT PRIMARY KEY) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1),(2),(3),(4);
 
CREATE TABLE t2 (entityKey INT, xhtmltext TEXT, FULLTEXT(xhtmltext)) ENGINE=InnoDB;
INSERT INTO t2 VALUES (1,"silver"),(2,"gold"),(3,"gold"),(4,"gold");
 
SELECT t1.entityKey FROM t1 
WHERE t1.entityKey IN (
  SELECT t2.entityKey FROM t2 
  WHERE MATCH (t2.xhtmltext) AGAINST ('gold')
) 
ORDER BY t1.entityKey DESC;
 
# Cleanup
DROP TABLE t1, t2;

Actual result from the 2nd test case

MariaDB [test]> SELECT t1.entityKey FROM t1 
    -> WHERE t1.entityKey IN (
    ->   SELECT t2.entityKey FROM t2 
    ->   WHERE MATCH (t2.xhtmltext) AGAINST ('gold')
    -> ) 
    -> ORDER BY t1.entityKey DESC;
+-----------+
| entityKey |
+-----------+
|         4 |
|         4 |
|         4 |
+-----------+
3 rows in set (0.00 sec)

With optimizer_switch=orderby_uses_equalities=off both test cases return expected results.

Comment by Jose Costa Teixeira [ 2017-09-25 ]

Hi. The workaround does not work in my specific case. I will have to wait for a fix.

Thanks!

Comment by Varun Gupta (Inactive) [ 2018-04-20 ]

It is possibly a duplicate of MDEV-13694 which is in review

Generated at Thu Feb 08 08:07:42 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.