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

Removed unnecessary left join and distinct #36813

Merged
merged 1 commit into from
Feb 20, 2022
Merged

Removed unnecessary left join and distinct #36813

merged 1 commit into from
Feb 20, 2022

Conversation

n3t
Copy link
Contributor

@n3t n3t commented Jan 24, 2022

Summary of Changes

left joined table b is not used in query at all, it just generates multiple same rows, which are solved by distinct. Removing both gives the same result and removes 'using temporary table' and 'Range checked for each record' from query explain, which could cause high performance issues on some servers.

Plus add quoting on table name.

Testing Instructions

Enable Debug mode, open list of articles, see generated Explain for query

SELECT DISTINCT a.id AS value, a.path, a.title AS text, a.level, a.published, a.lft 
FROM sdx54_tags AS a 
LEFT JOIN `sdx54_tags` AS b ON a.lft > b.lft AND a.rgt < b.rgt 
WHERE `a`.`lft` > 0 AND a.published IN (0, 1) ORDER BY a.lft ASC

Actual result BEFORE applying this Pull Request

Tags in filetr field for Tags are displayed, sorted by it ordering
Explain of query shows

Using where; Using temporary; Using filesort
Range checked for each record (index map: 0x10); Distinct

before

Expected result AFTER applying this Pull Request

Tags in filetr field for Tags are displayed, sorted by it ordering
Query is simplified to

SELECT a.id AS value, a.path, a.title AS text, a.level, a.published, a.lft 
FROM `sdx54_tags` AS a 
WHERE `a`.`lft` > 0 AND a.published IN (0, 1) ORDER BY a.lft ASC

Explain shows

Using where; Using filesort

after

Documentation Changes Required

No

Additional info

This issue remains in 4.0 and 4.1, If accepted I could prepare PR there also.

In my case this becomes issue after moving from MySQL 5 to MySQL 8 on some server. the original query took aprox. 8s, new one is aprox 5ms.

left joined table b is not used in query at all, it just generates multiple same rows, which are solved by distinct. Removing both gives the same result and removes 'using temporary table' from query explain, which could cause high performance issues on some servers.

Plus add quoting on table name.
@chmst
Copy link
Contributor

chmst commented Jan 24, 2022

There is a longer history for this select clause - #4114

@n3t
Copy link
Contributor Author

n3t commented Jan 24, 2022

Vow, I didn't search so far in history.

Ok, as far as I understand, #4114 replaced 'group by' by distinct, but neither of it is IMO needed, as it is used just to remove duplicate records, loaded just because there is left join, which is not needed, as there is no usage of table b neither in select list fields, nor where clause nor order by clause.

Just tested with PostgreSQL 14.1, and works fine
postgres

@alikon
Copy link
Contributor

alikon commented Feb 12, 2022

I have tested this item ✅ successfully on 16d58d5


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/36813.

1 similar comment
@PhocaCz
Copy link
Contributor

PhocaCz commented Feb 17, 2022

I have tested this item ✅ successfully on 16d58d5


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/36813.

@Quy
Copy link
Contributor

Quy commented Feb 17, 2022

RTC


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/36813.

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Feb 17, 2022
@zero-24 zero-24 merged commit 839404b into joomla:3.10-dev Feb 20, 2022
@joomla-cms-bot joomla-cms-bot removed the RTC This Pull Request is Ready To Commit label Feb 20, 2022
@zero-24
Copy link
Contributor

zero-24 commented Feb 20, 2022

Merging here thanks

@zero-24 zero-24 added this to the Joomla 3.10.7 milestone Feb 20, 2022
@n3t n3t deleted the n3t-tagfield-patch branch February 20, 2022 13:00
@Quy Quy mentioned this pull request Feb 20, 2022
nibra pushed a commit to nibra/joomla-cms that referenced this pull request Mar 22, 2022
left joined table b is not used in query at all, it just generates multiple same rows, which are solved by distinct. Removing both gives the same result and removes 'using temporary table' from query explain, which could cause high performance issues on some servers.

Plus add quoting on table name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants