-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
SQL queries performance enhancement #9266
SQL queries performance enhancement #9266
Conversation
…perator with case insensetive function 'ilike'
public class ThingsboardPostgreSQLDialect extends org.hibernate.dialect.PostgreSQL10Dialect { | ||
public ThingsboardPostgreSQLDialect() { | ||
super(); | ||
registerFunction("ilike", new SQLFunctionTemplate(BooleanType.INSTANCE, "(?1 ILIKE ?2)")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should ilike
always be in lowercase or can we use ILIKE
too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can use ILIKE too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide an example of the SQL statement that is generated based on @query and this dialect. I would like to double check that the final SQL statement uses ILIKE without any overhead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SQL test property
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
I believe
It is an excessive parenthesis.
Hibernate:
select
deviceenti0_.id as id1_18_,
deviceenti0_.created_time as created_2_18_,
deviceenti0_.additional_info as addition3_18_,
deviceenti0_.customer_id as customer4_18_,
deviceenti0_.device_data as device_d5_18_,
deviceenti0_.device_profile_id as device_p6_18_,
deviceenti0_.external_id as external7_18_,
deviceenti0_.firmware_id as firmware8_18_,
deviceenti0_.label as label9_18_,
deviceenti0_.name as name10_18_,
deviceenti0_.software_id as softwar11_18_,
deviceenti0_.tenant_id as tenant_12_18_,
deviceenti0_.type as type13_18_
from
device deviceenti0_
where
deviceenti0_.tenant_id=?
and (
(
deviceenti0_.name ILIKE ('%'||?||'%')
)=true
or (
deviceenti0_.label ILIKE ('%'||?||'%')
)=true
)
order by
deviceenti0_.id asc nulls last limit ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without parenthesis
registerFunction("ilike", new SQLFunctionTemplate(StandardBasicTypes.BOOLEAN, "?1 ILIKE ?2"));
from
device deviceenti0_
where
deviceenti0_.tenant_id=?
and (
deviceenti0_.name ILIKE ('%'||?||'%')=true
or deviceenti0_.label ILIKE ('%'||?||'%')=true
)
order by
deviceenti0_.id asc nulls last limit ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I waiting so long for such an improvement. Good job!
In general, the PR will bring some performance for big datasets.
But it still will use full scan, and full-text indexes will not help to search by many columns (OR clause).
The HQL dialect looks valid. I don't like to use a custom hql function, but I can not provide you with a better implementation
@@ -139,3 +139,5 @@ queue.rule-engine.queues[2].partitions=2 | |||
queue.rule-engine.queues[2].processing-strategy.retries=1 | |||
queue.rule-engine.queues[2].processing-strategy.pause-between-retries=0 | |||
queue.rule-engine.queues[2].processing-strategy.max-pause-between-retries=0 | |||
|
|||
spring.jpa.properties.hibernate.dialect=org.thingsboard.server.dao.ThingsboardPostgreSQLDialect |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public class ThingsboardPostgreSQLDialect extends org.hibernate.dialect.PostgreSQL10Dialect { | ||
public ThingsboardPostgreSQLDialect() { | ||
super(); | ||
registerFunction("ilike", new SQLFunctionTemplate(BooleanType.INSTANCE, "(?1 ILIKE ?2)")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SQL test property
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
I believe
It is an excessive parenthesis.
Hibernate:
select
deviceenti0_.id as id1_18_,
deviceenti0_.created_time as created_2_18_,
deviceenti0_.additional_info as addition3_18_,
deviceenti0_.customer_id as customer4_18_,
deviceenti0_.device_data as device_d5_18_,
deviceenti0_.device_profile_id as device_p6_18_,
deviceenti0_.external_id as external7_18_,
deviceenti0_.firmware_id as firmware8_18_,
deviceenti0_.label as label9_18_,
deviceenti0_.name as name10_18_,
deviceenti0_.software_id as softwar11_18_,
deviceenti0_.tenant_id as tenant_12_18_,
deviceenti0_.type as type13_18_
from
device deviceenti0_
where
deviceenti0_.tenant_id=?
and (
(
deviceenti0_.name ILIKE ('%'||?||'%')
)=true
or (
deviceenti0_.label ILIKE ('%'||?||'%')
)=true
)
order by
deviceenti0_.id asc nulls last limit ?
public class ThingsboardPostgreSQLDialect extends org.hibernate.dialect.PostgreSQL10Dialect { | ||
public ThingsboardPostgreSQLDialect() { | ||
super(); | ||
registerFunction("ilike", new SQLFunctionTemplate(BooleanType.INSTANCE, "(?1 ILIKE ?2)")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without parenthesis
registerFunction("ilike", new SQLFunctionTemplate(StandardBasicTypes.BOOLEAN, "?1 ILIKE ?2"));
from
device deviceenti0_
where
deviceenti0_.tenant_id=?
and (
deviceenti0_.name ILIKE ('%'||?||'%')=true
or deviceenti0_.label ILIKE ('%'||?||'%')=true
)
order by
deviceenti0_.id asc nulls last limit ?
Pull Request description
Added null-check for search text in sql queries and replaced 'LIKE' operator with case insensetive function 'ilike'.
General checklist
Front-End feature checklist
Back-End feature checklist