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

explain tidb_mdl_view contains CARTESIAN #46933

Closed
together-wang opened this issue Sep 13, 2023 · 7 comments · Fixed by #48728
Closed

explain tidb_mdl_view contains CARTESIAN #46933

together-wang opened this issue Sep 13, 2023 · 7 comments · Fixed by #48728
Assignees
Labels
component/ddl This issue is related to DDL of TiDB. found/gs found by gs type/enhancement The issue or PR belongs to an enhancement.

Comments

@together-wang
Copy link

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

tiup playground v6.5.4
mysql --comments --host 127.0.0.1 --port 4000 -u root

	CreateMDLView = `CREATE OR REPLACE VIEW mysql.tidb_mdl_view as (
		SELECT job_id,
			db_name,
			table_name,
			query,
			session_id,
			txnstart,
			tidb_decode_sql_digests(all_sql_digests, 4096) AS SQL_DIGESTS
		FROM information_schema.ddl_jobs,
			information_schema.cluster_tidb_trx,
			information_schema.cluster_processlist
		WHERE (ddl_jobs.state != 'synced' and ddl_jobs.state != 'cancelled')
			AND Find_in_set(ddl_jobs.table_id, cluster_tidb_trx.related_table_ids)
			AND cluster_tidb_trx.session_id = cluster_processlist.id
	);`

CreateMDLView = `CREATE OR REPLACE VIEW mysql.tidb_mdl_view as (

mysql> explain select * from mysql.tidb_mdl_view;
+-------------------------------------+-------------+-----------+---------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id                                  | estRows     | task      | access object             | operator info                                                                                                                                                                                                                           |
+-------------------------------------+-------------+-----------+---------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Projection_14                       | 99900000.00 | root      |                           | Column#1, Column#2, Column#3, Column#13, information_schema.cluster_tidb_trx.session_id, information_schema.cluster_processlist.txnstart, tidb_decode_sql_digests(information_schema.cluster_tidb_trx.all_sql_digests, 4096)->Column#43 |
| └─Projection_15                     | 99900000.00 | root      |                           | Column#1, Column#2, Column#3, Column#13, information_schema.cluster_tidb_trx.session_id, information_schema.cluster_tidb_trx.all_sql_digests, information_schema.cluster_processlist.txnstart                                           |
|   └─HashJoin_17                     | 99900000.00 | root      |                           | CARTESIAN inner join, other cond:find_in_set(cast(Column#7, var_string(20)), information_schema.cluster_tidb_trx.related_table_ids)                                                                                                     |
|     ├─HashJoin_18(Build)            | 12487.50    | root      |                           | inner join, equal:[eq(information_schema.cluster_tidb_trx.session_id, information_schema.cluster_processlist.id)]                                                                                                                       |
|     │ ├─TableReader_24(Build)       | 10000.00    | root      |                           | data:TableFullScan_23                                                                                                                                                                                                                   |
|     │ │ └─TableFullScan_23          | 10000.00    | cop[tidb] | table:CLUSTER_PROCESSLIST | keep order:false, stats:pseudo                                                                                                                                                                                                          |
|     │ └─TableReader_22(Probe)       | 9990.00     | root      |                           | data:Selection_21                                                                                                                                                                                                                       |
|     │   └─Selection_21              | 9990.00     | cop[tidb] |                           | not(isnull(information_schema.cluster_tidb_trx.session_id))                                                                                                                                                                             |
|     │     └─TableFullScan_20        | 10000.00    | cop[tidb] | table:CLUSTER_TIDB_TRX    | keep order:false, stats:pseudo                                                                                                                                                                                                          |
|     └─Selection_25(Probe)           | 8000.00     | root      |                           | eq(Column#12, "running")                                                                                                                                                                                                                |
|       └─MemTableScan_26             | 10000.00    | root      | table:DDL_JOBS            |                                                                                                                                                                                                                                         |
+-------------------------------------+-------------+-----------+---------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
11 rows in set (0.01 sec)

2. What did you expect to see? (Required)

The execution plan contains CARTESIAN, and as the production ddl increases, the view query becomes slower and slower. Optimization is recommended.

3. What did you see instead (Required)

mysql> explain select * from mysql.tidb_mdl_view;
+-------------------------------------+-------------+-----------+---------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id                                  | estRows     | task      | access object             | operator info                                                                                                                                                                                                                           |
+-------------------------------------+-------------+-----------+---------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Projection_14                       | 99900000.00 | root      |                           | Column#1, Column#2, Column#3, Column#13, information_schema.cluster_tidb_trx.session_id, information_schema.cluster_processlist.txnstart, tidb_decode_sql_digests(information_schema.cluster_tidb_trx.all_sql_digests, 4096)->Column#43 |
| └─Projection_15                     | 99900000.00 | root      |                           | Column#1, Column#2, Column#3, Column#13, information_schema.cluster_tidb_trx.session_id, information_schema.cluster_tidb_trx.all_sql_digests, information_schema.cluster_processlist.txnstart                                           |
|   └─HashJoin_17                     | 99900000.00 | root      |                           | CARTESIAN inner join, other cond:find_in_set(cast(Column#7, var_string(20)), information_schema.cluster_tidb_trx.related_table_ids)                                                                                                     |
|     ├─HashJoin_18(Build)            | 12487.50    | root      |                           | inner join, equal:[eq(information_schema.cluster_tidb_trx.session_id, information_schema.cluster_processlist.id)]                                                                                                                       |
|     │ ├─TableReader_24(Build)       | 10000.00    | root      |                           | data:TableFullScan_23                                                                                                                                                                                                                   |
|     │ │ └─TableFullScan_23          | 10000.00    | cop[tidb] | table:CLUSTER_PROCESSLIST | keep order:false, stats:pseudo                                                                                                                                                                                                          |
|     │ └─TableReader_22(Probe)       | 9990.00     | root      |                           | data:Selection_21                                                                                                                                                                                                                       |
|     │   └─Selection_21              | 9990.00     | cop[tidb] |                           | not(isnull(information_schema.cluster_tidb_trx.session_id))                                                                                                                                                                             |
|     │     └─TableFullScan_20        | 10000.00    | cop[tidb] | table:CLUSTER_TIDB_TRX    | keep order:false, stats:pseudo                                                                                                                                                                                                          |
|     └─Selection_25(Probe)           | 8000.00     | root      |                           | eq(Column#12, "running")                                                                                                                                                                                                                |
|       └─MemTableScan_26             | 10000.00    | root      | table:DDL_JOBS            |                                                                                                                                                                                                                                         |
+-------------------------------------+-------------+-----------+---------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
11 rows in set (0.01 sec)

4. What is your TiDB version? (Required)

v6.5.4
ALL

@together-wang together-wang added the type/bug The issue is confirmed as a bug. label Sep 13, 2023
@ti-chi-bot ti-chi-bot bot added may-affects-5.3 This bug maybe affects 5.3.x versions. may-affects-5.4 This bug maybe affects 5.4.x versions. may-affects-6.1 may-affects-6.5 may-affects-7.1 labels Sep 13, 2023
@winoros
Copy link
Member

winoros commented Sep 13, 2023

find_in_set can not perform as join key.
So there's a CARTESIAN product.
It's correct for the optimizer.
But maybe this SQL can be rewritten.

@winoros winoros added sig/sql-infra SIG: SQL Infra and removed sig/planner SIG: Planner labels Sep 13, 2023
@lcwangchao lcwangchao added the component/ddl This issue is related to DDL of TiDB. label Sep 14, 2023
@lcwangchao
Copy link
Collaborator

@wjhuang2016 PTAL

@bb7133 bb7133 removed the sig/sql-infra SIG: SQL Infra label Sep 15, 2023
@seiya-annie
Copy link

/found gs

@ti-chi-bot ti-chi-bot bot added the found/gs found by gs label Sep 20, 2023
@jebter
Copy link

jebter commented Sep 21, 2023

/label affects-6.5

@ti-chi-bot ti-chi-bot bot added affects-6.5 This bug affects the 6.5.x(LTS) versions. and removed may-affects-6.5 labels Sep 21, 2023
@D3Hunter
Copy link
Contributor

D3Hunter commented Oct 18, 2023

workaround if querying tidb_mdl_view is slow, create a new view using this:

limition: only works for DDL with only 1 table, more than 1 won't work, such as exchange partition, rename multiple tables using rename table. (tidb_mdl_view might not working for this case too, see #47743)

CREATE OR REPLACE VIEW new_tidb_mdl_view as (
    SELECT job_id,
        json_extract(cast(cast(job_meta as char) as json), '$.schema_name') as db_name,
        json_extract(cast(cast(job_meta as char) as json), '$.table_name') as table_name,
        json_extract(cast(cast(job_meta as char) as json), '$.query') as query,
        session_id,
        txnstart,
        tidb_decode_sql_digests(all_sql_digests, 4096) AS SQL_DIGESTS
    FROM mysql.tidb_ddl_job,
        information_schema.cluster_tidb_trx,
        information_schema.cluster_processlist
    WHERE tidb_ddl_job.processing = 1
        AND Find_in_set(json_extract(cast(cast(job_meta as char) as json), '$.table_id'), cluster_tidb_trx.related_table_ids)
        AND cluster_tidb_trx.session_id = cluster_processlist.id
);

@ti-chi-bot ti-chi-bot added affects-7.5 This bug affects the 7.5.x(LTS) versions. affects-6.1 This bug affects the 6.1.x(LTS) versions. affects-7.1 This bug affects the 7.1.x(LTS) versions. and removed may-affects-6.1 may-affects-7.1 labels Oct 23, 2023
@Benjamin2037 Benjamin2037 removed the affects-7.5 This bug affects the 7.5.x(LTS) versions. label Nov 8, 2023
@wjhuang2016
Copy link
Member

It's not a bug. So I remove the bug label.

@wjhuang2016 wjhuang2016 removed type/bug The issue is confirmed as a bug. severity/major labels Nov 22, 2023
@wjhuang2016 wjhuang2016 removed may-affects-5.3 This bug maybe affects 5.3.x versions. may-affects-5.4 This bug maybe affects 5.4.x versions. affects-6.1 This bug affects the 6.1.x(LTS) versions. affects-6.5 This bug affects the 6.5.x(LTS) versions. affects-7.1 This bug affects the 7.1.x(LTS) versions. labels Nov 22, 2023
@Benjamin2037 Benjamin2037 added the type/enhancement The issue or PR belongs to an enhancement. label Dec 19, 2023
@kennedy8312
Copy link

candidate_1m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/ddl This issue is related to DDL of TiDB. found/gs found by gs type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.