-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
getCount(). Perfs issues for multi columns primary key #5314
Comments
I haven't used any multi-column keys, can you try this? const query: SelectQueryBuilder<TEntity> = dbConnection
.getRepository<TEntity>(entity)
.createQueryBuilder(entity.name)
.select("COUNT(*)", "count")
.where(`"${entity.name}"."${id}"= :val`, {
val: value.toLowerCase(),
});
const totalRecords: number = await query.getRawOne().then(r => r.count); This is how i perform my queries, but i'm also using it in a mixed scenario of count/sum/avg so i use the getOneRaw quite a bit. |
Hi Chris, Will close the issue later on, in case some team guys designing typeorm (which is a fantastic tool btw) want to give us some clues Rgds This is how I have refactored the query :
|
@jeromeSH26 i 100% agree that the getCount function should be flexible/adjustable, unfortunately i'm not a maintainer on this project. |
Yep, that's why I leave this question opened for a while |
Note this also causes incorrect counts to be returned. Here is an example. You have two number columns set as primary keys - The following two rows will return the incorrect count.
Count should be 2, but returns 1. |
In cases with joins this won't work. If there are no joins it's possible to be a performance improvement we could apply but we'd need a number of tests to validate behavior. |
currently we use concatenation of multiple primary keys and a COUNT DISTINCT of that to figure out how many records we have matched in a query. however, that fails if the records have keys when the keys are ambigious when concatenated (`"A", "AA"` & `"AA", "A"`) the fact that we do a distinct can also be a performance impact that isn't needed when we aren't doing joins as such, in MySQL & Postgres we can use the built in counting of multiple distinct values to resolve some of the issues, and in other environments we can make it SLIGHTLY better by adding delimiters between the concatenated values. It is not perfect because it technically could run into the same issue if the delimiters are in the primary keys but it's BETTER in most cases. also, in cases where we do not perform any joins we can short circuit all of this and do a much more performant `COUNT(1)` operation fixes typeorm#5989 fixes typeorm#5314 fixes typeorm#4550
currently we use concatenation of multiple primary keys and a COUNT DISTINCT of that to figure out how many records we have matched in a query. however, that fails if the records have keys when the keys are ambigious when concatenated (`"A", "AA"` & `"AA", "A"`) the fact that we do a distinct can also be a performance impact that isn't needed when we aren't doing joins as such, in MySQL & Postgres we can use the built in counting of multiple distinct values to resolve some of the issues, and in other environments we can make it SLIGHTLY better by adding delimiters between the concatenated values. It is not perfect because it technically could run into the same issue if the delimiters are in the primary keys but it's BETTER in most cases. also, in cases where we do not perform any joins we can short circuit all of this and do a much more performant `COUNT(1)` operation fixes typeorm#5989 fixes typeorm#5314 fixes typeorm#4550
Can you confirm that #6870 fixes the issues you're seeing? |
currently we use concatenation of multiple primary keys and a COUNT DISTINCT of that to figure out how many records we have matched in a query. however, that fails if the records have keys when the keys are ambigious when concatenated (`"A", "AA"` & `"AA", "A"`) the fact that we do a distinct can also be a performance impact that isn't needed when we aren't doing joins as such, in MySQL & Postgres we can use the built in counting of multiple distinct values to resolve some of the issues, and in other environments we can make it SLIGHTLY better by adding delimiters between the concatenated values. It is not perfect because it technically could run into the same issue if the delimiters are in the primary keys but it's BETTER in most cases. also, in cases where we do not perform any joins we can short circuit all of this and do a much more performant `COUNT(1)` operation fixes #5989 fixes #5314 fixes #4550
) currently we use concatenation of multiple primary keys and a COUNT DISTINCT of that to figure out how many records we have matched in a query. however, that fails if the records have keys when the keys are ambigious when concatenated (`"A", "AA"` & `"AA", "A"`) the fact that we do a distinct can also be a performance impact that isn't needed when we aren't doing joins as such, in MySQL & Postgres we can use the built in counting of multiple distinct values to resolve some of the issues, and in other environments we can make it SLIGHTLY better by adding delimiters between the concatenated values. It is not perfect because it technically could run into the same issue if the delimiters are in the primary keys but it's BETTER in most cases. also, in cases where we do not perform any joins we can short circuit all of this and do a much more performant `COUNT(1)` operation fixes typeorm#5989 fixes typeorm#5314 fixes typeorm#4550
Issue type:
[X ] question
Database system/driver:
[X ]
postgres
Version 0.2.18
I'm facing a perf issue when running getCount()
Why when counting all records of a query, queryBuilder creates a SQL query that concatenates the columns that are part of the primary key ?
Typescript :
generated sql
That is destroying the performances for a table of 140K records.
Creating an index on CONCAT using is not efficient, hard to maintain with 100 of tables, knowing that for some tables, some primay comumns are timestamp...
Is there a way or a parameter to set to avoid the concat and just running a kind of (
select count(1) FROM "sc"."tbl" "u"
) ?Rgds
The text was updated successfully, but these errors were encountered: