Assuming your only problem is the choice, not the insert / update / delete, it depends on the needs, such as:
- Do you need to know what level each node is at?
- Do you need to know if each node has any children when rendering it?
- Need to sort your siblings by name?
However, if there are really minimal changes on the tree, then it doesnβt matter which circuit you use, since you can do all the work at the application level and cache the output.
Edit:
When order matters, I usually use the materialized path method and add an extra SortPath column. This way you can get results sorted by sister, which is denormalization, which makes rendering the tree in HTML extremely simple, since you can write the whole tree (or any part) exactly in the order in which you get the records using one query, This is optimal for speed, and it is the easiest way to sort more than one level at a time.
eg.
CREATE TABLE [dbo].[MatPath]( [ID] [int] NULL, [Name] [varchar](50) NULL, [Path] [varchar](max) NULL, [SortPath] [varchar](max) NULL ) insert into MatPath (ID, Name, Path, SortPath) values (1, 'Animal', '1', 'Animal-1') insert into MatPath (ID, Name, Path, SortPath) values (2, 'Dog', '1.2', 'Animal-1|Dog-2') insert into MatPath (ID, Name, Path, SortPath) values (3, 'Horse', '1.3', 'Animal-1|Horse-3') insert into MatPath (ID, Name, Path, SortPath) values (4, 'Beagle', '1.2.4', 'Animal-1|Dog-2|Beagle-4') insert into MatPath (ID, Name, Path, SortPath) values (5, 'Abyssinian', '1.3.5', 'Animal-1|Horse-3|Abyssinian-5') insert into MatPath (ID, Name, Path, SortPath) values (6, 'Collie', '1.2.6', 'Animal-1|Dog-2|Collie-6') select * from MatPath order by SortPath
Output:
ID Name Path SortPath ------ --------------- ----------- -------------------------------- 1 Animal 1 Animal-1 2 Dog 1.2 Animal-1|Dog-2 4 Beagle 1.2.4 Animal-1|Dog-2|Beagle-4 6 Collie 1.2.6 Animal-1|Dog-2|Collie-6 3 Horse 1.3 Animal-1|Horse-3 5 Abyssinian 1.3.5 Animal-1|Horse-3|Abyssinian-5 (6 row(s) affected)
You can determine the level of each node by counting pipes (or periods) at the application level or in SQL using any built-in or user-defined functions that can count occurrences of a string .
In addition, you will notice that I am adding ID to Name when creating SortPath . This is to ensure that two siblings with the same name always return in the same order.