Firebird SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesFirebird SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 22nd, 2004, 09:08 AM
graefalexander graefalexander is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 5 graefalexander User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question FireBird Subqueries

Hello alltogether,

I'm currently evaluating FireBird for writting some complex queries. But there are some queries, which I used to use with other databases (MSSQL, SQLite), which wont work with Firebird. In my case, I absolutely need the option to do a subquery in this form:

Code:
SELECT A.Col1, A.Col2 FROM
(SELECT Col1, Col2 FROM Table1) AS A;


or to get a real world example:

Code:
SELECT A.Artist, A.Title FROM
(SELECT Albums.Artist, Albums.Title FROM Albums) AS A;


This query runs quite fine in SQLite, but FireBird moans about the second "SELECT" in the query. The example above doesnt make much sense, but I need a GROUB BY in a complex query, and in SQLite I could solve that with wrapping a GROUP BY arround the whole query:

Code:
SELECT A.Title FROM
(SELECT ...complex query with multiple joins and one union...) AS A
GROUP BY A.Title;


I'm curious if FireBird doesnt understand using SELECTs as table references.

Thanks in advance, Alex

Reply With Quote
  #2  
Old November 22nd, 2004, 09:35 AM
shammat shammat is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 1,007 shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 13 h 20 m 10 sec
Reputation Power: 67
The official name is "derived table" and currently Firebird does not support this. AFAIK this is planned for Firebird 2.0

Regards
Thomas

Reply With Quote
  #3  
Old November 22nd, 2004, 11:18 AM
graefalexander graefalexander is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 5 graefalexander User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by shammat
The official name is "derived table" and currently Firebird does not support this. AFAIK this is planned for Firebird 2.0


Thanks a lot, i found this dedicated term (derived table) nowhere on the net. Any information on plans when FireBird 2.0 (at least release candidates) will be published? I'm in active development on a free project, and I want to know if I better switch to another database, or better temporary cut off some of the functions (in this case, its only the search result ranking for which I need this query) until the new version has been released.

Thank you for your time and reply, Alex

Reply With Quote
  #4  
Old November 22nd, 2004, 11:38 AM
shammat shammat is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 1,007 shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 13 h 20 m 10 sec
Reputation Power: 67
As long as you don't have any dynamic conditions in the derived table, you can always replace them with a view. Much easier to read anyway:
Code:
CREATE VIEW v1 AS SELECT ...complex query with multiple joins and one union...;

SELECT Title FROM
v1
GROUP BY Title;
This will also be portable to all other DBMS (that support views).

Regarding FB 2.0:
http://www.firebirdsql.com/index.php?op=faq#q0007.dat

In a nutshell: "around Easter 2005"

Regards
Thomas

Reply With Quote
  #5  
Old November 22nd, 2004, 12:06 PM
graefalexander graefalexander is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 5 graefalexander User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile

Quote:
Originally Posted by shammat
As long as you don't have any dynamic conditions in the derived table


You've guessed it, it's a search function, which joins an index created by Triggers/SPs with the content tables. It's like this:

Code:
SELECT Albums.* FROM Tokenize('Albums Title') Tokenize
       INNER JOIN Tokens ON Tokenize.Token = Tokens.Token
       INNER JOIN Index_Albums ON Tokens.TokenID = Index_Albums.TokenID
       INNER JOIN Albums ON Index_Albums.AlbumID = Albums.AlbumID


I already thought about creating a view, which I would drop after fetching all records. However, this seems too ugly to me, and the query runs currently lighting fast, I dont want to make it slow by using brute-force-creating-and-dropping-views.

Quote:
Originally Posted by shammat
Regarding FB 2.0:
http://www.firebirdsql.com/index.php?op=faq#q0007.dat

In a nutshell: "around Easter 2005"


OK, I can wait for it. Without ranking, the search function is little bit useless, but thats not a problem for further development, and until the application will be in a beta stage, FireBird will hopefully reach 2.0.

Thank you for your time, Alex

Reply With Quote
  #6  
Old November 23rd, 2004, 07:11 AM
fikret fikret is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Sarajevo, Bosnia
Posts: 111 fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 5 h 32 m 19 sec
Reputation Power: 8
Or you can try to use fyracle www.fyracle.org. It's Oracle mode firebird. Base is Firebird 1.5 BUT with derived tables support backported from firebird 2.0.

Reply With Quote
  #7  
Old November 23rd, 2004, 09:42 AM
graefalexander graefalexander is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 5 graefalexander User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by fikret
Or you can try to use fyracle www.fyracle.org. It's Oracle mode firebird. Base is Firebird 1.5 BUT with derived tables support backported from firebird 2.0.


Thanks for your advice. Seeing that this company was able to backport the derived tables support from the Firebird 2.0 development branch is a sign that this feature will also be included in one of the first RCs for the new Firebird.

However, the project I'm working on is open source, written in C#. The main features of Firebird I'm interested in are the small footprint and the embedded database. If I switch to another database server, I would not likely choose a Firebird-derivate, but instead PostgreSQL (which has an excellent ADO.NET provider).

The query I need to perform can be rewritten, I've done that already, but then the performance is real cruel. For the time beeing, I can simply use the query without ranking, test it with the slow ranking, and then, when Firebird 2.0 is available, switch to the fast ranking query which needs derived tables support.

For anyone curious about the query (thats the slow one), here we go:

Code:
SELECT Albums.Title, Albums.TrackCount, COUNT(*) FROM Tracks
       INNER JOIN Albums ON Tracks.AlbumID = Albums.AlbumID
WHERE
     Albums.AlbumID IN (
         SELECT AlbumID FROM Tokens
             INNER JOIN Index_Albums ON Tokens.TokenID = Index_Albums.TokenID
             WHERE Token = 'Aphex' OR Token = 'Twin'
     ) OR
     Tracks.TrackID IN (
         SELECT TrackID FROM Tokens
             INNER JOIN Index_Tracks ON Tokens.TokenID = Index_Tracks.TokenID
             WHERE Token = 'Aphex' OR Token = 'Twin'
     ) AND
     (Albums.Artist CONTAINING 'Aphex Twin' OR
     Tracks.Artist CONTAINING 'Aphex Twin')
GROUP BY Albums.Title, Albums.TrackCount;


This is so slow, that even SQLite, utilizing a derived tables query an no fulltext index table is more than 10 times faster. Anyway, the result is correct.

The inner query can be transformed into a JOIN:

Code:
SELECT * FROM Tokenize('Apehx Twin') Tokenize
      INNER JOIN Tokens ON Tokenize.Token = Tokens.Token
      INNER JOIN Index_Tracks ON Tokens.TokenID = Index_Tracks.TokenID
      INNER JOIN Tracks ON Index_Tracks.TrackID = Tracks.TrackID
      INNER JOIN Albums ON Tracks.AlbumID = Albums.AlbumID


This query is lightning fast, mainly because the first table on which the query joins is the table generated by Tokenize (a stored procedure which splits strings on whitespace and returns each substring as a row), which is quite small (2 records). But if I want to search in albums and tracks together, I need an union:

Code:
SELECT
      Albums.AlbumID,...
FROM Tokenize('Aphex Twin') Tokenize
       INNER JOIN Tokens ON Tokenize.Token = Tokens.Token
       INNER JOIN Index_Tracks ON Tokens.TokenID = Index_Tracks.TokenID
       INNER JOIN Tracks ON Index_Tracks.TrackID = Tracks.TrackID
       INNER JOIN Albums ON Tracks.AlbumID = Albums.AlbumID
       LEFT OUTER JOIN Discs ON Tracks.DiscID = Discs.DiscID
WHERE
       Albums.Artist CONTAINING 'Aphex Twin' OR
       Tracks.Artist CONTAINING 'Aphex Twin'
GROUP BY
      Albums.AlbumID, ...
UNION
SELECT
      Albums.AlbumID,...
      FROM Tokenize('Aphex Twin') Tokenize
       INNER JOIN Tokens ON Tokenize.Token = Tokens.Token
       INNER JOIN Index_Albums ON Tokens.TokenID = Index_Albums.TokenID
       INNER JOIN Albums ON Index_Albums.AlbumID = Albums.AlbumID
       INNER JOIN Tracks ON Index_Albums.AlbumID = Tracks.AlbumID
       LEFT OUTER JOIN Discs ON Tracks.DiscID = Discs.DiscID
WHERE
       Albums.Artist CONTAINING 'Aphex Twin' OR
       Tracks.Artist CONTAINING 'Aphex Twin'
GROUP BY
      Albums.AlbumID, ...;


Every queries from the union selects all tracks matching the criteria. Both queries can return duplicates, and I would need to eleminate these, in order to get the grouping with the ranking factor (which is how many of the total tracks in the album do match the criteria). Seems I'm stuck and can only wait for FB2.0, so that i can group a derived table.

Regards, Alex

Reply With Quote
  #8  
Old November 23rd, 2004, 02:25 PM
shammat shammat is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 1,007 shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 13 h 20 m 10 sec
Reputation Power: 67
Quote:
The main features of Firebird I'm interested in are the small footprint and the embedded database. If I switch to another database server, I would not likely choose a Firebird-derivate, but instead PostgreSQL

Postgres is a fine database, but there is no embedded version of it, and it requires a real "installation" as opposed to Firebird which can be simply copied to the harddrive (or as part of the application)

Thomas

Reply With Quote
  #9  
Old November 24th, 2004, 04:17 AM
fikret fikret is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Sarajevo, Bosnia
Posts: 111 fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 5 h 32 m 19 sec
Reputation Power: 8
Quote:
Originally Posted by graefalexander
Thanks for your advice. Seeing that this company was able to backport the derived tables support from the Firebird 2.0 development branch is a sign that this feature will also be included in one of the first RCs for the new Firebird.
...
Regards, Alex

I would suggest you that you post your question on support group on yahoo or on news server nntp://news.atkin.com.

--
Best regards,
Fikret Hasovic http://fikret.fbtalk.net
TAMP R&D Team
FirebirdSQL Foundation member.
- Join today at http://www.firebirdsql.org/ff/foundation
JEDI VCS contributor
http://jedivcs.sourceforge.net/

Reply With Quote
  #10  
Old November 24th, 2004, 07:23 AM
graefalexander graefalexander is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 5 graefalexander User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by shammat
Postgres is a fine database, but there is no embedded version of it, and it requires a real "installation" as opposed to Firebird which can be simply copied to the harddrive (or as part of the application)

Thomas


Yes, thats right. This is why I want to stick with Firebird, even if I have to wait for another Release until all needed features are implemented.

But if I would be forced to switch to another database, I would not switch to a Firebird derivate (which is, as far as I can see, a database server, not simply an engine), but instead to a more powerful database.

Please note, that there is no embedded release for Firebird on Linux, which is the second target platform. Installation of Firebird is quite easy, and installation of PostgreSQL is sometimes challenging, so I'll stick with Firebird. Anyway, I did write much code for Firebird yet, much more than for any other database I evaluated, so I would like to stay with Firebird.

Regards, Alex

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > FireBird Subqueries


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |