Transferred from: loveyuki-SQL server storage-level data for unlimited Classification

Source: Internet
Author: User
Tags how to use sql server how to use sql
SQL Server storage-level data for unlimited Classification

Because the data stored in the database is stored in a plane, most of the forums and otherProgramIt uses recursion to display hierarchical data. If the classification level is very deep, the number of recursion times is considerable, and the performance is also greatly affected. This problem has been encountered recently when we want to build a platform for classification information. How can we quickly display hierarchical data? MySQL developers help us think of an algorithm.AlgorithmCurrently, the only problem is that classification sorting has not yet been implemented. We can use reverse sorting of the right value to achieve first-in-first-out sorting. Here we need to know how to use SQL Server for implementation. We will use the provincial, municipal, and county databases as an example:

We can see the numbers on the left and right of a tree node.RuleIf the left and right values of Shanxi are (8, 17), all nodes with the left value greater than 8 and the right value less than 17 belong to the sub-nodes of Shanxi. If the left and right values are (), then all of his parent nodes are nodes with a left value less than 14 and a right value greater than 15, the unlimited classification performance achieved by this method is absolutely superb. You can find the data of a node and its sub-nodes in one query. This is an unlimited classification algorithm with the highest performance I have ever seen. Other algorithms have no advantage in comparison with this algorithm.

First, create a data table with a structure such as (lid is the left value, RID is the right value, tree is the node depth, name and ID are not much said, node index and name)

We can use the following stored procedure to obtain a node and Its subnodes:

 
 
  1. Create ProcedureClsp_zoneselect
  2. (
  3. @ RootInt,
  4. @ TreeInt 
  5. )
  6. As 
  7. SelectZ. ID, Z. Tree, Z.Name
  8. FromCl_zonedataAsZ, cl_zonedataAsP
  9. WhereP. ID = @ Root
  10. AndZ. lid> = P. LidAndZ. Rid <= P. RID
  11. And(@ Tree = 0OrZ. Tree <= P. Tree + @ tree)
  12. Order ByZ. LidASC 
  13. Go

We can use the following stored procedure to insert a new subnode under a node:

 
 
  1. Create ProcedureClsp_zoneinsert
  2. (
  3. @ RootInt,
  4. @NameNvarchar (50)
  5. )
  6. As 
  7. Declare@ RIDAs Int, @ NidAs Int, @ TreeAs Int 
  8.  
  9. Set@ Rid = 1
  10. Set@ Nid = 0
  11. Set@ Tree = 1
  12.  
  13. If @ root = 0
  14. Begin 
  15. Select Top1 @ rid = rid + 1
  16. FromCl_catedataOrder ByRIDDesc 
  17. End 
  18. Else 
  19. Begin 
  20. Select@ Rid = RID, @ tree = tree + 1
  21. FromCl_zonedataWhereId = @ Root
  22. End 
  23.  
  24. If @ root = 0Or@ Rid> 1
  25. Begin 
  26. UpdateCl_zonedataSetRid = rid + 2WhereRid> = @ RID
  27. UpdateCl_zonedataSetLid = lid + 2WhereLid> @ RID
  28.  
  29. Insert IntoCl_zonedata (lid, RID, tree,Name)
  30. Values(@ RID, @ rid + 1, @ tree ,@Name)
  31.  
  32. Set@ Nid = scope_identity ()
  33. End 
  34. Select@ Nid
  35. Go

You can use the following stored procedure to delete a node:

 
 
  1. Create ProcedureClsp_zonedelete
  2. (
  3. @ IDInt 
  4. )
  5. As 
  6. Declare@ LidAs Int, @ RIDAs Int, @ WidAs Int, @ DidAs Int 
  7. Set@ Did = 0
  8. Select@ Did = ID, @ lid = lid, @ rid = RID, @ WID = RID-lid + 1FromCl_zonedataWhereId = @ ID
  9. If @ did! = 0
  10. Begin 
  11. Delete FromCl_zonedataWhereLidBetween@ LidAnd@ RID
  12. UpdateCl_zonedataSetRid = RID-@ widWhereRid> @ RID
  13. UpdateCl_zonedataSetLid = lid-@ widWhereLid> @ RID
  14. End 
  15. Select@ Did
  16. Go
This Article Edited by loveyuki
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.