Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance range calculation upon user variables #8733

Open
laizhebujum opened this issue Dec 18, 2018 · 11 comments
Open

enhance range calculation upon user variables #8733

laizhebujum opened this issue Dec 18, 2018 · 11 comments
Labels
challenge-program component/expression help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. sig/planner SIG: Planner type/enhancement The issue or PR belongs to an enhancement.

Comments

@laizhebujum
Copy link

laizhebujum commented Dec 18, 2018

Description

Compare two sqls
1、
SET @execDate = DATE_FORMAT(NOW(),'%Y-%m-%d');
SET @start_time = DATE_ADD(NOW(), INTERVAL -1 DAY);
SET @end_time = @execDate;
desc SELECT count(*) FROM GameActiveLogDB.wendao_active_log WHERE active_time >= @Start_time AND active_time < @End_time;

image

2、desc SELECT count(*) FROM GameActiveLogDB.wendao_active_log WHERE active_time >= DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY),'%Y-%m-%d') AND active_time < DATE_FORMAT(NOW(),'%Y-%m-%d');
image

Score

900

SIG slack channel(must):

Contact us in channel #sig-planner of TiDB Community

Mentor(must)

@lysu lysu added type/bug The issue is confirmed as a bug. sig/planner SIG: Planner labels Dec 18, 2018
@lysu
Copy link
Contributor

lysu commented Dec 18, 2018

Thanks for your feedback, we will take a look @laizhebujum

@zz-jason
Copy link
Member

confirmed that this problem still exists in release 4.0 and the latest master branch at this time:

TiDB(root@127.0.0.1:test) > create table t(a bigint, b bigint, index idx(a));
Query OK, 0 rows affected (0.00 sec)

TiDB(root@127.0.0.1:test) > desc select * from t where a > 1 and a < 10;
+-------------------------------+---------+-----------+----------------------------------------------------------------+
| id                            | estRows | task      | operator info                                                  |
+-------------------------------+---------+-----------+----------------------------------------------------------------+
| IndexLookUp_10                | 250.00  | root      |                                                                |
| ├─IndexRangeScan_8(Build)     | 250.00  | cop[tikv] | table:t, index:a, range:(1,10), keep order:false, stats:pseudo |
| └─TableRowIDScan_9(Probe)     | 250.00  | cop[tikv] | table:t, keep order:false, stats:pseudo                        |
+-------------------------------+---------+-----------+----------------------------------------------------------------+
3 rows in set (0.00 sec)

TiDB(root@127.0.0.1:test) > set @a = 1;
Query OK, 0 rows affected (0.00 sec)

TiDB(root@127.0.0.1:test) > set @b = 10;
Query OK, 0 rows affected (0.00 sec)

TiDB(root@127.0.0.1:test) > desc select * from t where a > @a and a < @b;
+-------------------------+----------+-----------+------------------------------------------------------------------------------+
| id                      | estRows  | task      | operator info                                                                |
+-------------------------+----------+-----------+------------------------------------------------------------------------------+
| Selection_5             | 8000.00  | root      | gt(cast(test.t.a), cast(getvar("a"))), lt(cast(test.t.a), cast(getvar("b"))) |
| └─TableReader_7         | 10000.00 | root      | data:TableFullScan_6                                                         |
|   └─TableFullScan_6     | 10000.00 | cop[tikv] | table:t, keep order:false, stats:pseudo                                      |
+-------------------------+----------+-----------+------------------------------------------------------------------------------+
3 rows in set (0.01 sec)

@zz-jason zz-jason changed the title Maybe it's a bug to use variable assignment, to execute a plan without indexing enhance range calculation upon user variables Mar 18, 2020
@zz-jason zz-jason removed their assignment Mar 18, 2020
@zz-jason zz-jason added help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. type/enhancement The issue or PR belongs to an enhancement. and removed type/bug The issue is confirmed as a bug. labels Mar 18, 2020
@eurekaka
Copy link
Contributor

eurekaka commented Sep 29, 2020

This is because GetVar in the filter is not folded into a constant, so ranger cannot extract it as access condition and build range for it. Currently, GetVar is marked as unFoldableFunctions on purpose, to handle the cases like:

select @a := @a +1 from t, (select @a := 0) var;

We can only fold the GetVar into a constant if the query contains no SetVar for the same user variable. We'd better try to optimize this after #18973 is merged.

@asiafrank
Copy link
Contributor

/pick-up

@ti-challenge-bot
Copy link

Pick up success.

@ti-challenge-bot
Copy link

@asiafrank You did not submit PR within 7 days, so give up automatically.

@ti-challenge-bot ti-challenge-bot bot removed the picked label Nov 9, 2020
@asiafrank
Copy link
Contributor

/pick-up

@ti-challenge-bot
Copy link

Pick up success.

@ti-challenge-bot
Copy link

@asiafrank You did not submit PR within 7 days, so give up automatically.

@ti-challenge-bot ti-challenge-bot bot removed the picked label Nov 17, 2020
@asiafrank
Copy link
Contributor

/pick-up

@ti-challenge-bot
Copy link

Pick up success.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
challenge-program component/expression help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. sig/planner SIG: Planner type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
7 participants