You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to get adverts with attachments(advert has_many attachments). If I use query without pagination:
@adverts = Advert.where(category_id: params[:id]).includes(:attachments).references(:attachments)
I get result in single query.
SELECT adverts.id AS t0_r0, adverts.created_at AS t0_r1, adverts.updated_at AS t0_r2, adverts.category_id AS t0_r3, adverts.title AS t0_r4, adverts.body AS t0_r5, adverts.user_id AS t0_r6, adverts.visible AS t0_r7, adverts.attachments_count AS t0_r8, attachments.id AS t1_r0, attachments.image AS t1_r1, attachments.created_at AS t1_r2, attachments.updated_at AS t1_r3, attachments.advert_id AS t1_r4 FROM adverts LEFT OUTER JOIN attachments ON attachments.advert_id = adverts.id WHERE adverts.category_id = 1
It supposes if I use pagination - the result will be in two queries(one is adverts with attachments, second count of adverts). But I get three queries: with limit, with attachments, and count.
SELECT DISTINCT adverts.id FROM adverts LEFT OUTER JOIN attachments ON attachments.advert_id = adverts.id WHERE adverts.category_id = 1 LIMIT 10 OFFSET 0
SELECT adverts.id AS t0_r0, adverts.created_at AS t0_r1, adverts.updated_at AS t0_r2, adverts.category_id AS t0_r3, adverts.title AS t0_r4, adverts.body AS t0_r5, adverts.user_id AS t0_r6, adverts.visible AS t0_r7, adverts.attachments_count AS t0_r8, attachments.id AS t1_r0, attachments.image AS t1_r1, attachments.created_at AS t1_r2, attachments.updated_at AS t1_r3, attachments.advert_id AS t1_r4 FROM adverts LEFT OUTER JOIN attachments ON attachments.advert_id = adverts.id WHERE adverts.category_id = 1 AND adverts.id IN (130, 131, 132, 136, 144, 150, 151, 153, 155, 158)
SELECT COUNT(DISTINCT adverts.id) FROM adverts LEFT OUTER JOIN attachments ON attachments.advert_id = adverts.id WHERE adverts.category_id = 1
Is it possible to get rid of extra query?
The text was updated successfully, but these errors were encountered:
I'm trying to get adverts with attachments(advert has_many attachments). If I use query without pagination:
@adverts = Advert.where(category_id: params[:id]).includes(:attachments).references(:attachments)
I get result in single query.
SELECT
adverts
.id
AS t0_r0,adverts
.created_at
AS t0_r1,adverts
.updated_at
AS t0_r2,adverts
.category_id
AS t0_r3,adverts
.title
AS t0_r4,adverts
.body
AS t0_r5,adverts
.user_id
AS t0_r6,adverts
.visible
AS t0_r7,adverts
.attachments_count
AS t0_r8,attachments
.id
AS t1_r0,attachments
.image
AS t1_r1,attachments
.created_at
AS t1_r2,attachments
.updated_at
AS t1_r3,attachments
.advert_id
AS t1_r4 FROMadverts
LEFT OUTER JOINattachments
ONattachments
.advert_id
=adverts
.id
WHEREadverts
.category_id
= 1It supposes if I use pagination - the result will be in two queries(one is adverts with attachments, second count of adverts). But I get three queries: with limit, with attachments, and count.
@adverts = Advert.where(category_id: params[:id]).includes(:attachments).references(:attachments).page(params[:page])
SELECT DISTINCT
adverts
.id
FROMadverts
LEFT OUTER JOINattachments
ONattachments
.advert_id
=adverts
.id
WHEREadverts
.category_id
= 1 LIMIT 10 OFFSET 0SELECT
adverts
.id
AS t0_r0,adverts
.created_at
AS t0_r1,adverts
.updated_at
AS t0_r2,adverts
.category_id
AS t0_r3,adverts
.title
AS t0_r4,adverts
.body
AS t0_r5,adverts
.user_id
AS t0_r6,adverts
.visible
AS t0_r7,adverts
.attachments_count
AS t0_r8,attachments
.id
AS t1_r0,attachments
.image
AS t1_r1,attachments
.created_at
AS t1_r2,attachments
.updated_at
AS t1_r3,attachments
.advert_id
AS t1_r4 FROMadverts
LEFT OUTER JOINattachments
ONattachments
.advert_id
=adverts
.id
WHEREadverts
.category_id
= 1 ANDadverts
.id
IN (130, 131, 132, 136, 144, 150, 151, 153, 155, 158)SELECT COUNT(DISTINCT
adverts
.id
) FROMadverts
LEFT OUTER JOINattachments
ONattachments
.advert_id
=adverts
.id
WHEREadverts
.category_id
= 1Is it possible to get rid of extra query?
The text was updated successfully, but these errors were encountered: