|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
The official name is "derived table" and currently Firebird does not support this. AFAIK this is planned for Firebird 2.0
Regards Thomas |
|
#3
|
|||
|
|||
|
Quote:
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 |
|
#4
|
|||
|
|||
|
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; Regarding FB 2.0: http://www.firebirdsql.com/index.php?op=faq#q0007.dat In a nutshell: "around Easter 2005" Regards Thomas |
|
#5
|
||||
|
||||
|
Quote:
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:
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 |
|
#6
|
|||
|
|||
|
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.
|
|
#7
|
|||
|
|||
|
Quote:
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 |
|
#8
|
|||
|
|||
|
Quote:
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 |
|
#9
|
|||
|
|||
|
Quote:
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/ |
|
#10
|
|||
|
|||
|
Quote:
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 |
![]() |
| Viewing: Dev Shed Forums > Databases > Firebird SQL Development > FireBird Subqueries |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|