Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a subquery from the FROM field in MySQL

I'm new to MySQL and databases and I've seen in many places that it is not considered a good programming practice to use subqueries in the FROM field of SELECT in MySQL, like this:

select userid, avg(num_pages) 
from (select userid , pageid , regid , count(regid) as num_pages 
      from reg_pag 
      where ativa = 1 
      group by userid, pageid) as from_query 
group by userid;

which calculates the average number of registers per page that the users have.

The reg_page table looks like this: reg_page table image

Questions:

  1. How to change the query so that it doesn't use the subquery in the FROM field?

  2. And is there a general way to "flatten" queries like this?

like image 509
Lidia Freitas Avatar asked Dec 05 '15 02:12

Lidia Freitas


People also ask

How do you avoid subquery in SELECT statement?

Change the EXISTS statement to a JOIN statement to avoid nested subqueries and reduce the execution time from 1.93 seconds to 1 millisecond.

How do you enclose a sub query?

Subqueries must be enclosed within parentheses. A subquery can have only one column in the SELECT clause, unless multiple columns are in the main query for the subquery to compare its selected columns.

Can DELETE have sub queries?

DELETE operations with subqueries that reference the same table object are supported only if all of the following conditions are true: The subquery either returns a single row, or else has no correlated column references. The subquery is in the DELETE statement WHERE clause, using Condition with Subquery syntax.


1 Answers

The average number of registers per page per user can also be calculated as number of registers per user divided by number of pages per user. Use count distinct to count only distinct psgeids per user:

select userid, count(regid) / count(distinct pageid) as avg_regs
from reg_pag 
where ativa=1
group by userid;

There is no general way of flattening such queries. It may not even be possible to flatten some of them, otherwise there would be little point in having this feature in the first place. Do not get scared of using subqueries in the from clause, in some occasions they may be even more effective, than a flattened query. But this is vendor and even version specific.

like image 163
Shadow Avatar answered Nov 01 '22 13:11

Shadow