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.
Status | New | ⇒ | Information Required |
Closed_By | franz-wohlkoenig | ⇒ | joomla-cms-bot |
Status | Information Required | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2019-03-03 09:58:07 |
Closed_By | ⇒ | franz-wohlkoenig |
Set to "closed" on behalf of @franz-wohlkoenig by The JTracker Application at issues.joomla.org/joomla-cms/23983
@artur-stepien as there is no Answer i close this one, it can always reopened.
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.
@artur-stepien do you have benchmarks or any information about b/c
@alikon can you check this suggestion?
@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.
ok please go forward and create a PR against 4.0-dev thx
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'
schema is sometimes really slow, especially on older mysql versions.
maybe this one is better
SHOW KEYS FROM table WHERE Key_name = 'PRIMARY'
works on all supported versions? At least it looks really simple.
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.
@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.
ok so can you please provide a PR?
@HLeithner I need a week or so. Plenty of projects to finish.
no problem we have some time left till J4 beta ;-)
The name of the function is just misleading.
@artur-stepien absolutely yes, sorry for the misunderstand, looking forward to your PR
l
if its faster its good, if you like to create a PR you are welcome please do this against j4 branch.