14
Well, I usually just work with frameworks. I work with frameworks MVC and I usually use Orms to query the database.
In my case I use Laravel, but I’ve used other frameworks, and had the opportunity recently to meet the Entity Framework.
Some experienced users regarding the use of the Entity Framework showed me that the ORM of the same usually generates an SQL for query other than the one we are used to (something that someone too serious could say that decreases the performance).
In the same way I have already noted this in the framework Laravel.
What I mean is that in some cases, these frameworks usually generate some queries with Subqueries, instead of using a JOIN
.
For example, I’ve seen one query that, to identify if a user has any publication, was being generated in this way:
select * from
usuarios
where
(select count(*) from `posts` where `usuarios`.`id` = `posts`.`usuario_id`) >= 1
But I believe that the same consultation could be done with JOIN.
Well, now I explain why I’m talking about frameworks before getting into the subject. It is because I have heard several comments from the mouths of "programmers", who say that Subqueries causes performance problems. But what you can’t understand is why frameworks then they would insist on doing something that was harmful (reducing performance is harmful, I meant in that context).
However, if I see being used in frameworks, soon comes to mind that "no problem at all" use Subqueries. And without any problem, would not understand so why the developers of frameworks, who theoretically would have enough experience to know what they’re doing.
I an issue on Github, where there was someone reporting the performance problem because of the Subqueries generated, I noticed that the library developer asked, "But does your tables contain the indexes set correctly?" which led me to understand that the problem can sometimes be more in the wrong way of using than properly in the subquery.
So, based on all my arguments above, I ask:
Subqueries at all times cause performance problems, or this problem may occur depending on a specific context?
It is true that setting indexes for a table can improve the performance of a subquery or is that bullshit?
The query will show above, in the example, if done otherwise (as for example, using
JOIN
), would have improved performance?
only a detail that does not answer your question, but helps you improve it. SQL shown is not a valid subquery example. There is no way to do this in SQL, at least in the Dbms I know.
– cantoni
Thanks @Cantoni, good observation
– Wallace Maxters
I was surprised to learn that the Laravel generates a query "nut" of these. If I want to know a user’s posts would do only: SELECT COUNT(POST_ID) FROM POSTS WHERE PUBLISHER_ID = 100 for example. It would count for one field, reducing the amount of bytes per tuple obviously eliminating *. It wouldn’t even need Join because I want to know the amount of posts of active and non-active users, anything just pass an extra attribute in Where and everything solved. Amazing how they complicate what should be so simple!
– Mateus
In relation to frameworks (and some OO projects that use a lot of OO) sometimes, maintaining the standard is not the best performance case. Writing object-oriented to achieve performance is in itself a difficult task. Often, you have an experienced programmer who writes a little performantico code because of design or culture. An important point about frameworks is that they can’t predict all the forms that will be used, but they try to generate code based on patterns of behavior and good design practices, so they can generate low performance code.
– Intruso