Dan Farino About MySpace’s Architecture
Dan Farino talks about the system architecture and the challenges faced when building a very large online community. Dan explains how a .NET product scales on hundreds of servers.
Tracking change and innovation in the enterprise software development community
Posted by Jonathan Allen on Oct 05, 2007 01:24 AM
Relational databases are great for storing most forms of structured data. The most notable exception is recursive data. Tree-like structures, essential for menus, normally require awkward stored procedures to efficiently return. SQL Server 2005 does have an answer though.
Common Table Expressions, or CTEs, were introduced in SQL Server 2005, but they have not been getting the attention they deserve. In their simplest form, they are a named sub-query that appear before the main query. Since the CTE can be used multiple times, it can make complex queries look significantly cleaner.
The syntax is easy to use, but does have a few points to remember.
;WITH FirstCTE (column1, column2) AS
(SELECT column1, column2 FROM MyTable),
SecondCTE(column1, column2) AS
(SELECT column1, column2 FROM OtherTable)
Select * FROM FirstCTE UNION ALL Select * FROM SecondCTE
Multiple CTEs can be chained together, but they must be used immediately after being defined; no other code can appear between the last CTE and the final query. The semi-colon is only required if the CTE isn't the first statement in the batch, but it is easier to use it every time. CTEs cannot use ORDER BYor COMPUTE, as they are essentially sub-queries.
CTEs becomes really interesting when used recursively. An article in October's MSDN Magazine outlines the rules of recursion.
For example,
;WITH MenuCTE(MenuKey, ParentMenuKey, MenuName) AS
(
-- Anchor Query
SELECT MenuKey, ParentMenuKey, MenuName FROM Menu WHERE MenuKey = 1
UNION ALL
-- Recursive Query
SELECT m.MenuKey, m.ParentMenuKey, m.MenuName FROM Menu m
INNER JOIN MenuCTE r ON m.ParentMenuKey = r.MenuKey
)
SELECT MenuKey, ParentMenuKey, MenuName FROM MenuCTE
Agile Development: A Manager’s Roadmap for Success
Webcast: Applying lean thinking to the governance of software development
IBM software architect eKit: Grady Booch podcast, whitepapers, articles
Nice post either way, but something I've never run across is even a shallow overview of the recursive solutions for several of the most popular databases.
As far as I know, Oracle has this with the SQL99 "WITH" clause. I think PostgreSQL may have a module to support the same syntax. I'm not aware of any recursive query support for MySQL.
A vendor-neutral alternative tactic I've seen is to use an ancestry path column, ie. a column on the row containing a path of ancestor IDs, eg "/1/2/3/4". It is then possible to do some fairly sophisticated querying/updating on this using just LIKE and wildcards.
Kit
Oracle has supported recursive SQLs for years with the "CONNECT BY" syntax:
select
node_id,
parent_node_id,
level -- pseudo-column
from
node
connect by
prior node_id = parent_node_id
start with
node_id = 'TOP';
I know this is a off topic but as far as SqlServer 2005 goes, I was a little disappointed when I discovered it still wasn't able to do triggers on self referencing tables. So, just a little bit of public exposure might make people at Redmond work a bit harder :)
forums.microsoft.com/TechNet/ShowPost.aspx?Post...
connect.microsoft.com/SQLServer/feedback/ViewFe...
To be honest, I simply don't have the time to keep up with all the database vendors. But if you feel your favorite databases are being slighted, let me know and I'll try to run more articles on them.
As for Oracle and CTEs, here is a link for our other readers.
www.dba-oracle.com/t_sql99_with_clause.htm
If anyone has something for other vendors, please post it here.
Well if you want to write a brief article on why it is needed, I'll be happy to run it as a "guest opinion". I can't promise it will help, but maybe we can garner a few more votes on Microsoft Connect.
Or the nested set model. Great for read heavy operations.
www.developersdex.com/gurus/articles/112.asp
i'm kinda outdated, and disapointed about the "WITH" syntax is not applicable in mysql.
anyone could give me a clue that the following codes in sql2005 translated in mysql?
thanks for respond.
******************************************************
WITH MenuCTE(MenuKey, ParentMenuKey, MenuName) AS
(
-- Anchor Query
SELECT MenuKey, ParentMenuKey, MenuName FROM Menu WHERE MenuKey = 1
UNION ALL
-- Recursive Query
SELECT m.MenuKey, m.ParentMenuKey, m.MenuName FROM Menu m
INNER JOIN MenuCTE r ON m.ParentMenuKey = r.MenuKey
)
SELECT MenuKey, ParentMenuKey, MenuName FROM MenuCTE
******************************************************
Dan Farino talks about the system architecture and the challenges faced when building a very large online community. Dan explains how a .NET product scales on hundreds of servers.
Bernd Mathiske discusses Maxine VM, Java compatibility, swapping major VM components, research areas, Object handling, code examples, optimizing compiler, snippets, bytecode generation, JNI and JIT.
Joe Armstrong speaks on various aspects of the Erlang language, presenting its roots, how it compares with other languages and why it has become popular these days.
The java double-check singleton pattern is not thread safe and can’t be fixed. In this article, Dr. Alexey Yakubovich provides an implementation of the Singleton pattern that he claims is thread-safe.
Diana and Jim talk about patterns observed in CTOs' activity. CTOs emerge as real people caring for other people in their organization, and are put under a lot of pressure and constraints.
Cloud computing feels like a tomorrow technology. Simon Thurman shows how developers can use Biztalk to create an Internet Service Bus which can be deployed locally or in the cloud.
InfoQ takes a look at the JavaFX preview build and talks to Sun Staff Engineer Joshua Marinacci about the upcoming version 1 release expected this autumn.
Jeff Sutherland, co-creator of Scrum, and Guido Schoonheim, CTO of Xebia, present an actual case of reaching hyper-productivity with a large distributed team using XP and Scrum.
8 comments
Reply