简体   繁体   中英

LIMIT changing sort order on query

Two requests with differing LIMIT number are yielding different sorting results.

SELECT "acgti"."id" AS "id", "acgti"."ac_node_id" AS "ac_node_id", "acgti"."ac_msn_number" AS "ac_msn_number", "acgti"."ac_production_site" AS "ac_production_site_short_name", "acgti"."ac_program_id" AS "ac_program_id", "acgti"."ac_fal_rank" AS "ac_fal_rank", "acgti"."ac_status" AS "ac_status", "acgti"."logical_station_production_site" AS "logical_station_production_site", "acgti"."logical_station_short_name" AS "logical_station_short_name", "acgti"."rgti_node_id" AS "rgti_node_id", "acgti"."rgti_release_number" AS "rgti_release", "acgti"."rgti_reference" AS "rgti_reference", "acgti"."rgti_title" AS "rgti_title", "acgti"."rgti_title_second_lang" AS "rgti_title_second_lang", "acgti"."status" AS "status", "acgti"."created_on" AS "created_on", "acgti"."updated_on" AS "updated_on", "acgti"."attestation_type" AS "attestation_type", "created_by"."fullname" AS "created_by", CONCAT("acgti"."ac_customer_version_name", '/', "acgti"."ac_customer_version_rank") AS "ac_version_and_rank" FROM "ac_gti" "acgti" LEFT JOIN "ext_usr_user" "created_by" ON "created_by"."username"="acgti"."created_by" LEFT JOIN "ext_usr_user" "updated_by" ON "updated_by"."username"="acgti"."updated_by" WHERE "acgti"."ac_production_site" IN ('A350 TJN', 'A350 TLS') ORDER BY "acgti"."rgti_reference" ASC NULLS FIRST LIMIT 10

Yields :

在此处输入图片说明

SELECT "acgti"."id" AS "id", "acgti"."ac_node_id" AS "ac_node_id", "acgti"."ac_msn_number" AS "ac_msn_number", "acgti"."ac_production_site" AS "ac_production_site_short_name", "acgti"."ac_program_id" AS "ac_program_id", "acgti"."ac_fal_rank" AS "ac_fal_rank", "acgti"."ac_status" AS "ac_status", "acgti"."logical_station_production_site" AS "logical_station_production_site", "acgti"."logical_station_short_name" AS "logical_station_short_name", "acgti"."rgti_node_id" AS "rgti_node_id", "acgti"."rgti_release_number" AS "rgti_release", "acgti"."rgti_reference" AS "rgti_reference", "acgti"."rgti_title" AS "rgti_title", "acgti"."rgti_title_second_lang" AS "rgti_title_second_lang", "acgti"."status" AS "status", "acgti"."created_on" AS "created_on", "acgti"."updated_on" AS "updated_on", "acgti"."attestation_type" AS "attestation_type", "created_by"."fullname" AS "created_by", CONCAT("acgti"."ac_customer_version_name", '/', "acgti"."ac_customer_version_rank") AS "ac_version_and_rank" FROM "ac_gti" "acgti" LEFT JOIN "ext_usr_user" "created_by" ON "created_by"."username"="acgti"."created_by" LEFT JOIN "ext_usr_user" "updated_by" ON "updated_by"."username"="acgti"."updated_by" WHERE "acgti"."ac_production_site" IN ('A350 TJN', 'A350 TLS') ORDER BY "acgti"."rgti_reference" ASC NULLS FIRST LIMIT 20

Yields different order :

在此处输入图片说明

We couldn't figure why, what is going on here?

You are using ORDER BY "acgti"."rgti_reference" ASC NULLS FIRST LIMIT 20 .

Now if there are some result rows where "acgti"."rgti_reference" IS NULL , these go first in the result set, and their order is not determined.

Add a second NOT NULL column to the ORDER BY clause as a tie-breaker to get a deterministic result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM