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

perf improvement opportunity for exists #290

Open
davidAtInleague opened this issue Oct 8, 2024 · 0 comments
Open

perf improvement opportunity for exists #290

davidAtInleague opened this issue Oct 8, 2024 · 0 comments

Comments

@davidAtInleague
Copy link
Contributor

davidAtInleague commented Oct 8, 2024

exists current implementation is

public boolean function exists( struct options = {} ) {
    return count( options = arguments.options ) > 0;
}

Which produces sql like

SELECT COALESCE(COUNT(*), 0) AS "aggregate" FROM [table] WHERE <<clauses>>

But this forces a count operation, and then does an inequality comparison in application code. In MSSQL, and I imagine all other database engines, the database cannot optimize away the count to just a "check that some row exists".

I think that exists could do better to emit:

--MSSqlserver grammar, but I believe this is all standard sql features
select case when exists (
  select * from [table] where <<clauses>>
) then 1 else 0 end as [doesItExist]

and the CF implementation would be

public boolean function exists( struct options = {} ) {
    return <<run exists statement>>.doesItExist == 1
}

Which would allow the database engine to just check if a record exists or not.

In the query I am looking at now, the count version is typically ~100ms, and the exists version is ~50ms. This change will probably fluctuate based on available indexes and complexity of the where clause, but in general I contend that the exists version is more appropriate here.

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

No branches or pull requests

1 participant