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

sortBy and take/drop resulting in wrong SQL #1160

Closed
schmitch opened this issue Jun 10, 2015 · 8 comments
Closed

sortBy and take/drop resulting in wrong SQL #1160

schmitch opened this issue Jun 10, 2015 · 8 comments

Comments

@schmitch
Copy link
Contributor

Hello when running a sortBy(_.id.desc).take(1) the resulting sql will be totally wrong.
It will be a SELECT columns FROM (SUBQUERY) instead of the wished SELECT columns FROM table ORDER BY x LIMIT 1;

@cvogt
Copy link
Member

cvogt commented Jun 10, 2015

does it produce different results? what is the sub query?

@schmitch
Copy link
Contributor Author

Currently the Query that will be generated is:

select x2."id", x2."oa_nr", x2."data", x2."old_id", x2."username" from (select x3."oa_nr" as "oa_nr", x3."username" as "username", x3."data" as "data", x3."id" as "id", x3."old_id" as "old_id" from "article_article" x3 limit 1) x2 order by x2."id" desc

The result is correct it's just that the Query is way slower due to the subquery..

@cvogt
Copy link
Member

cvogt commented Jun 10, 2015

That's a known issue when using Slick with MySQL. Neither Slicks nor MySQL's optimizers currently optimize this away. Works well with other dbs AFAIK. Slick 3.1 will come with an improved optimizer that produces the SQL you expected.

@cvogt cvogt closed this as completed Jun 10, 2015
@cvogt
Copy link
Member

cvogt commented Jun 10, 2015

#1133

@cvogt
Copy link
Member

cvogt commented Jun 10, 2015

"wrong" isn't really the case by the way. the produced query is semantically correct. SQL doesn't have execution semantics in the language, that's for the database to decide. The MySQL optimizer however didn't chose a good execution plan for this case and it is indeed possible to affect that by changing the structure of the query. but that is db and optimizer dependent and could even depend on runtime profiling data.

@schmitch
Copy link
Contributor Author

Good that I use PostgreSQL and still having a wrong query.
Semantically it's wrong. It's only right that they will result in the same value. If you run EXPLAIN ANALYZE you will see that a subquery with an order query will take a huge amount of time. especially when your data is not zero.

@cvogt
Copy link
Member

cvogt commented Jun 10, 2015

whats the performance difference?

@cvogt
Copy link
Member

cvogt commented Jun 10, 2015

I guess we disagree on the definition of semantics :). Anyways fixed in upcoming 3.1 or should already be in recent builds from master if I am not mistaken

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

2 participants