[MDEV-10922] optimization on count distinct on primary key Created: 2016-09-29  Updated: 2023-11-24

Status: Confirmed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.0, 10.1, 10.2
Fix Version/s: 10.2

Type: Bug Priority: Major
Reporter: Puggan Se Assignee: Sergei Petrunia
Resolution: Unresolved Votes: 0
Labels: None
Environment:

Package: mariadb-server
Version: 10.0.27-0ubuntu0.16.04.1


Issue Links:
Duplicate
is duplicated by MDEV-30660 COUNT DISTINCT seems unnecessarily sl... In Progress

 Description   

As all 3 queries just returns the number of rows in products, they should be able to run alot faster.

SELECT COUNT(our_article_id) FROM products; -- 0.17s
SELECT COUNT(DISTINCT our_article_id) FROM products; -- 0.99s
SELECT COUNT(DISTINCT our_article_id) FROM products LEFT JOIN product_article_id USING (our_article_id); -- 3.32s

CREATE TABLE products (
    our_article_id varchar(50) CHARACTER SET utf8 NOT NULL,
    PRIMARY KEY(our_article_id)
) ENGINE=InnoDB;
 
CREATE TABLE product_article_id (
    article_id varchar(255) COLLATE utf8_bin NOT NULL,
    our_article_id varchar(50) CHARACTER SET utf8 NOT NULL,
    PRIMARY KEY(article_id),
    INDEX(our_article_id)
) ENGINE=InnoDB;

// Add some data

INSERT INTO products VALUES ('a'), ('b'), ('c'), ('d'), ('e'), ('f'), ('g'), ('h'), ('i'), ('j'), ('k'), ('l'), ('m'), ('n'), ('o'), ('p'), ('q'), ('r'), ('s'), ('t'), ('u'), ('v'), ('w'), ('x'), ('y'), ('z');
INSERT INTO products SELECT CONCAT(a.our_article_id, b.our_article_id) FROM products AS a, products AS b;
INSERT IGNORE INTO product_article_id SELECT CONCAT(a.our_article_id, b.our_article_id), b.our_article_id FROM products AS a, products AS b;
INSERT IGNORE INTO products SELECT CONCAT(a.our_article_id, b.our_article_id) FROM products AS a, products AS b;

A count on a non-null-column on a table witout where-parts or joins, is always the number of rows in that table?

A distinct count non-null-uniqe-column on a table without where-parts, is always the number of rows in that table?

A distinct count on a table, is not influenced by a left join of another table, if that table have no where-parts?



 Comments   
Comment by Dipti Joshi (Inactive) [ 2016-09-29 ]

puggan This issue is on MariaDB Server and not on MariaDB ColumnStore. It should be filed with the project "MDEV" and not "MCOL" Moving it to MDEV project.

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