🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.interbase.sql
Article #846

Re: Performance: Best strategy for non-PK and non-alphabetical sort order for rows?

#846
From: Maxim Shiryaev
Date: Thu, 24 May 2007 13:52
56 lines
2344 bytes
As always I recommend to use SQL-implementd linked list.

You can add additional cloumn but not with integer to sort by but a pointer
to the next record.

Say you have
CREATE TABLE LINKED_LIST(
  ID INTEGER PRIMARY KEY,
  NAME VARCHAR(10),
  NEXT_ID INTEGER REFERENCES LINKED_LIST(ID)
)

Now to reorder your dataset you can change just one record.

To get data from the list you can use client-side proceudre or a stored
procedure.

Something like:

CREATE PRPCEDURE GET_LINKED_LIST_IN_ORDER
RETURNS (
  ID INTEGER
  NAME VARCHAR(10),
  NEXT_ID INTEGER
)
as
begin
  select id, name, next_id
  from LINKED_LIST
  where id is null /* start of the list */
  into :id, :name, :next_id;
  suspend;

  while (next_id is not null) do begin
    select id, name, next_id
    from LINKED_LIST
    where id = :next_id
    into :id, :name, :next_id;

    suspend;
  end
end

And you need to lock entier table to avoid missing chains or loops in your
list in a multi-user invironment.

Another optimization would be to separate ordering from the main data.
LINKED_LIST could containg only ID and NEXT_ID for the main table and the
procedure could combine them together. If you add to the LINKED_LIST a field
like USER_ID you could get multiple versions of ordering for different users
or situations without changing the main table.

But it works only if reordering is often. In other case it's better to use
sorting column.

Maxim.
Technical Details
Message-ID: <46559840$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <46540e25$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 114a0e,7ed325c4992c39f3
X-Google-Attributes: gid114a0e,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Maxim Shiryaev" <MaximShiryaev@gmail.com>
Newsgroups: borland.public.interbase.sql
References: <46540e25$1@newsgroups.borland.com>
Subject: Re: Performance: Best strategy for non-PK and non-alphabetical sort order for rows?
Date: Thu, 24 May 2007 17:52:21 +0400
Lines: 58
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
NNTP-Posting-Host: 213.234.197.62
Message-ID: <46559840$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1180014656 213.234.197.62 (24 May 2007 06:50:56 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail