Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. c# and sql recursive data query

c# and sql recursive data query

Scheduled Pinned Locked Moved C#
databasecsharpquestioncareer
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    peropata
    wrote on last edited by
    #1

    Hi, in my project I use:

    command = new SqlCommand(SQLStatement, DataBase.SqlDataReader
    reader = command.ExecuteReader();

    to query "part_ID" from large table. Each part_ID can have more subparts up to n level. They are all stored in same table. At the moment I use foreach statement to recursivly query parts from table until they have no sub parts. As there are up to 10000 subparts for some of parts it takes up to almost a minute to get all subparts (each query takes app. 15 ms). Is there a faster (more efficient) way to get the same job done?

    J N P M 4 Replies Last reply
    0
    • P peropata

      Hi, in my project I use:

      command = new SqlCommand(SQLStatement, DataBase.SqlDataReader
      reader = command.ExecuteReader();

      to query "part_ID" from large table. Each part_ID can have more subparts up to n level. They are all stored in same table. At the moment I use foreach statement to recursivly query parts from table until they have no sub parts. As there are up to 10000 subparts for some of parts it takes up to almost a minute to get all subparts (each query takes app. 15 ms). Is there a faster (more efficient) way to get the same job done?

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Yes, have a look at the Nested set model[^]

      1 Reply Last reply
      0
      • P peropata

        Hi, in my project I use:

        command = new SqlCommand(SQLStatement, DataBase.SqlDataReader
        reader = command.ExecuteReader();

        to query "part_ID" from large table. Each part_ID can have more subparts up to n level. They are all stored in same table. At the moment I use foreach statement to recursivly query parts from table until they have no sub parts. As there are up to 10000 subparts for some of parts it takes up to almost a minute to get all subparts (each query takes app. 15 ms). Is there a faster (more efficient) way to get the same job done?

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        Use a DataSet and fill it with each table and set a DataRelationship. Although it may use more memory depending on how much data you have it beats 10000 individual queries.


        I know the language. I've read a book. - _Madmatt

        1 Reply Last reply
        0
        • P peropata

          Hi, in my project I use:

          command = new SqlCommand(SQLStatement, DataBase.SqlDataReader
          reader = command.ExecuteReader();

          to query "part_ID" from large table. Each part_ID can have more subparts up to n level. They are all stored in same table. At the moment I use foreach statement to recursivly query parts from table until they have no sub parts. As there are up to 10000 subparts for some of parts it takes up to almost a minute to get all subparts (each query takes app. 15 ms). Is there a faster (more efficient) way to get the same job done?

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Could you show some of the schema?

          P 1 Reply Last reply
          0
          • P PIEBALDconsult

            Could you show some of the schema?

            P Offline
            P Offline
            peropata
            wrote on last edited by
            #5

            All queries are done from the same table (lets call it "parts_table"). Let say I start with "part_ID" = 1001 --> query returns 120 "subpart_id" (1002, 1004, 2030 ...) then i execute same query 120 times and get n subparts for each of 120 subpart_ID from first query and so on until there are no subparts left. As you can see the number of queries can increase very fast ... Table: "parts_table" id part_id subpart_id 1 1001 1002 2 1001 1004 3 1002 2150 4 1002 3250 5 1004 1250 . Part description for "part_id" is in separate table "part_info". I use join to get info for "part_id" ...

            P 1 Reply Last reply
            0
            • P peropata

              All queries are done from the same table (lets call it "parts_table"). Let say I start with "part_ID" = 1001 --> query returns 120 "subpart_id" (1002, 1004, 2030 ...) then i execute same query 120 times and get n subparts for each of 120 subpart_ID from first query and so on until there are no subparts left. As you can see the number of queries can increase very fast ... Table: "parts_table" id part_id subpart_id 1 1001 1002 2 1001 1004 3 1002 2150 4 1002 3250 5 1004 1250 . Part description for "part_id" is in separate table "part_info". I use join to get info for "part_id" ...

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              I'm still thinking about it, and one way (with SQL Server 2008) to reduce the number of queries/trips to the database is with a Table Valued Parameter.

              1 Reply Last reply
              0
              • P peropata

                Hi, in my project I use:

                command = new SqlCommand(SQLStatement, DataBase.SqlDataReader
                reader = command.ExecuteReader();

                to query "part_ID" from large table. Each part_ID can have more subparts up to n level. They are all stored in same table. At the moment I use foreach statement to recursivly query parts from table until they have no sub parts. As there are up to 10000 subparts for some of parts it takes up to almost a minute to get all subparts (each query takes app. 15 ms). Is there a faster (more efficient) way to get the same job done?

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                This is the reason we implemented the hierarchy id, using the partid in the hierarchy id has made a tremendous difference. So much so that I am taking the concept to an Oracle database. I would craft a stored proc that takes a part and returns all the children in one table, using a CTE to do the recursive loop (and I hate CTEs). Then to the presentation formatting in the business layer.

                Never underestimate the power of human stupidity RAH

                P 1 Reply Last reply
                0
                • M Mycroft Holmes

                  This is the reason we implemented the hierarchy id, using the partid in the hierarchy id has made a tremendous difference. So much so that I am taking the concept to an Oracle database. I would craft a stored proc that takes a part and returns all the children in one table, using a CTE to do the recursive loop (and I hate CTEs). Then to the presentation formatting in the business layer.

                  Never underestimate the power of human stupidity RAH

                  P Offline
                  P Offline
                  peropata
                  wrote on last edited by
                  #8

                  I am working with "SQL Server 2000 SP3 2000.80.760.0" and as I understand it does not support CTE. Are there any examples for recursive query for "SQL Server 2000" ?

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups