?
avatar artur-stepien
artur-stepien
22 Feb 2019

Additional comments

Wouldn't it be faster to use EXISTS instead of COUNT(*) in Joomla\CMS\Table\Table::hasPrimaryKey()? For a small tables difference isn't noticeable. But the higher count of rows in a table the bigger the time saved from using EXISTS(). MySQL/MariaDB uses that, MS SQL does too, even SQLLight does. I don't think that would make any difference for the core cause I didn't saw composite primary keys in Joomla! But for some of the external components that can make a difference in resources usage. Maybe it would make sense to create Joomla\CMS\Table\Table::exists(). Just something to think of.

avatar artur-stepien artur-stepien - open - 22 Feb 2019
avatar joomla-cms-bot joomla-cms-bot - labeled - 22 Feb 2019
avatar artur-stepien artur-stepien - change - 22 Feb 2019
The description was changed
avatar artur-stepien artur-stepien - edited - 22 Feb 2019
avatar HLeithner
HLeithner - comment - 22 Feb 2019

if its faster its good, if you like to create a PR you are welcome please do this against j4 branch.

avatar franz-wohlkoenig franz-wohlkoenig - change - 3 Mar 2019
Status New Information Required
avatar joomla-cms-bot joomla-cms-bot - change - 3 Mar 2019
Closed_By franz-wohlkoenig joomla-cms-bot
avatar joomla-cms-bot joomla-cms-bot - close - 3 Mar 2019
avatar franz-wohlkoenig franz-wohlkoenig - change - 3 Mar 2019
Status Information Required Closed
Closed_Date 0000-00-00 00:00:00 2019-03-03 09:58:07
Closed_By franz-wohlkoenig
avatar joomla-cms-bot
joomla-cms-bot - comment - 3 Mar 2019
avatar franz-wohlkoenig
franz-wohlkoenig - comment - 3 Mar 2019

@artur-stepien as there is no Answer i close this one, it can always reopened.


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

avatar artur-stepien
artur-stepien - comment - 3 Mar 2019

I can prepare this in a fast way by adding a string query to Table class. But I think this should be added to the Query object in a first place. Cause this is something that can be used in models of bigger components. I'll check it and let you know soon.

avatar HLeithner
HLeithner - comment - 3 Mar 2019

@artur-stepien do you have benchmarks or any information about b/c
@alikon can you check this suggestion?

avatar artur-stepien
artur-stepien - comment - 3 Mar 2019

@HLeithner There are benchmarks. Here is one http://sqlblog.com/blogs/andrew_kelly/archive/2007/12/15/exists-vs-count-the-battle-never-ends.aspx
In short. The more records in the table the more expensive COUNT(*) is. Cause COUNT requires from the server to look for every row matching query. Where EXISTS stops after the first one found.

And after I create the PR I will make a benchmark too to check if update will not do worse in Joomla! cause of framework overhead.

avatar HLeithner
HLeithner - comment - 3 Mar 2019

ok please go forward and create a PR against 4.0-dev thx

avatar alikon
alikon - comment - 4 Mar 2019

maybe we should simply look at schema

select constraint_name
from information_schema.table_constraints
where table_name = 'mytable'
and table_schema = 'myschema'
and constraint_name = 'PRIMARY'
avatar HLeithner
HLeithner - comment - 4 Mar 2019

schema is sometimes really slow, especially on older mysql versions.

avatar alikon
alikon - comment - 4 Mar 2019

maybe this one is better

SHOW KEYS FROM table WHERE Key_name = 'PRIMARY'
avatar HLeithner
HLeithner - comment - 4 Mar 2019

works on all supported versions? At least it looks really simple.

avatar artur-stepien
artur-stepien - comment - 5 Mar 2019

Why do you wan't to look for a schema if it the problem is completely different? Its about the fact, that Table uses COUNT(*) do determinate if the row exists. The name of the function is just misleading. We already know what is the primary key cause it is provided as a parameter of class extending Table. When autoincrement is disabled on the Table class it looks if there is a row with provided primary keys. And instead of using EXISTS() that just returns the fact if there is a row matching query, the COUNT(*) counts the rows (it access the data etc). Its is a small window where the change makes a performance improvement but it is there. That hasPrimaryKey is used when table uses a composite primary key. Now, I didn't saw a single core component using such keys. But it is often used on a big components and custom made components consisting of multiple tables. I will prepare the change and a testing method. Just give me time.

avatar artur-stepien
artur-stepien - comment - 5 Mar 2019

@HLeithner And as for the support. From what I already found it EXISTS() is implemented in MySQL/MariaDB, MS SQL, PostgreSQL and even SQLite. As for the version. Change shouldn't affect negatively any of the components cause function behavior is same. Just the method changing for a faster one.

avatar HLeithner
HLeithner - comment - 5 Mar 2019

ok so can you please provide a PR?

avatar artur-stepien
artur-stepien - comment - 5 Mar 2019

@HLeithner I need a week or so. Plenty of projects to finish.

avatar HLeithner
HLeithner - comment - 5 Mar 2019

no problem we have some time left till J4 beta ;-)

avatar alikon
alikon - comment - 5 Mar 2019

The name of the function is just misleading.

@artur-stepien absolutely yes, sorry for the misunderstand, looking forward to your PR

l

Add a Comment

Login with GitHub to post a comment