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. SQL UDF or C# function

SQL UDF or C# function

Scheduled Pinned Locked Moved C#
databasediscussionquestioncsharpsql-server
6 Posts 4 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.
  • E Offline
    E Offline
    eddieangel
    wrote on last edited by
    #1

    I have a database that houses (amongst other things) data on defects and repairs, car repairs as an example. The way this db breaks down is this: Table Defect holds the information about the defect (Damaged bumper, broken windshield, w/e) Table Material holds information about materials used to repair (ex: new bumper) Table Equipment holds information about equipment used to repair (ex: buffer) Table Trade holds information about who is responsible for the repair and their wage (ex: mechanic) There are three many to many tables that join defect with trade/eq/mat and have a quantity. Long story short, I have a Scalar value UDF that returns the value of all of these costs together divided by a value in the defect table. I decided to try to simplify it into a query (The simplification discussion is a TSQL issue outside the scope of this post) and in doing so had to ask myself, does this belong as a SQL query or should I write it as an additional property of the model in c#? I could just as easily write a partial class that handles all of this craziness on the web server side. Is this all pedantic? Is it really a question of db server performance vs web server performance? I have read that scalar value functions in TSQL can be low performing due to threading issues, which is what sparked this thing in the first place. Thoughts? Opinions? Cheers, --EA

    J M Kornfeld Eliyahu PeterK 3 Replies Last reply
    0
    • E eddieangel

      I have a database that houses (amongst other things) data on defects and repairs, car repairs as an example. The way this db breaks down is this: Table Defect holds the information about the defect (Damaged bumper, broken windshield, w/e) Table Material holds information about materials used to repair (ex: new bumper) Table Equipment holds information about equipment used to repair (ex: buffer) Table Trade holds information about who is responsible for the repair and their wage (ex: mechanic) There are three many to many tables that join defect with trade/eq/mat and have a quantity. Long story short, I have a Scalar value UDF that returns the value of all of these costs together divided by a value in the defect table. I decided to try to simplify it into a query (The simplification discussion is a TSQL issue outside the scope of this post) and in doing so had to ask myself, does this belong as a SQL query or should I write it as an additional property of the model in c#? I could just as easily write a partial class that handles all of this craziness on the web server side. Is this all pedantic? Is it really a question of db server performance vs web server performance? I have read that scalar value functions in TSQL can be low performing due to threading issues, which is what sparked this thing in the first place. Thoughts? Opinions? Cheers, --EA

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

      eddieangel wrote:

      Is it really a question of db server performance vs web server performance?

      Probably. If it is in fact a 'better' application if this specific feature is 'faster' and the speed up is achieved by one or the other (probably via TSQL) then that would be the deciding factor. Other than that it is conceptually business logic but given the complexity of the data entities then I would suspect that it is less complex (lower maintenance) to leave it in the database.

      eddieangel wrote:

      I have read that scalar value functions in TSQL can be low performing

      Which sounds irrelevant without information about the requirements, architecture, design and measured performance of the application (not benchmarks) related to this.

      E 1 Reply Last reply
      0
      • J jschell

        eddieangel wrote:

        Is it really a question of db server performance vs web server performance?

        Probably. If it is in fact a 'better' application if this specific feature is 'faster' and the speed up is achieved by one or the other (probably via TSQL) then that would be the deciding factor. Other than that it is conceptually business logic but given the complexity of the data entities then I would suspect that it is less complex (lower maintenance) to leave it in the database.

        eddieangel wrote:

        I have read that scalar value functions in TSQL can be low performing

        Which sounds irrelevant without information about the requirements, architecture, design and measured performance of the application (not benchmarks) related to this.

        E Offline
        E Offline
        eddieangel
        wrote on last edited by
        #3

        Thank you. You are right, "low performing" is immeasurable in the scope of my application. Because of the complexity of using a UDF with Entity Framework I am going to move the calculation to the web server side for the time being. Cheers, --EA

        1 Reply Last reply
        0
        • E eddieangel

          I have a database that houses (amongst other things) data on defects and repairs, car repairs as an example. The way this db breaks down is this: Table Defect holds the information about the defect (Damaged bumper, broken windshield, w/e) Table Material holds information about materials used to repair (ex: new bumper) Table Equipment holds information about equipment used to repair (ex: buffer) Table Trade holds information about who is responsible for the repair and their wage (ex: mechanic) There are three many to many tables that join defect with trade/eq/mat and have a quantity. Long story short, I have a Scalar value UDF that returns the value of all of these costs together divided by a value in the defect table. I decided to try to simplify it into a query (The simplification discussion is a TSQL issue outside the scope of this post) and in doing so had to ask myself, does this belong as a SQL query or should I write it as an additional property of the model in c#? I could just as easily write a partial class that handles all of this craziness on the web server side. Is this all pedantic? Is it really a question of db server performance vs web server performance? I have read that scalar value functions in TSQL can be low performing due to threading issues, which is what sparked this thing in the first place. Thoughts? Opinions? Cheers, --EA

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

          I would be asking myself where you are consuming the result, if it is ONLY in your UI then doing the calc in the model is valid. If however you intend to use it in reports then the database should be used. You may also consider storing the result somewhere if it is used in reports. Recalcing it every time you run a report may not be the best option.

          Never underestimate the power of human stupidity RAH

          E 1 Reply Last reply
          0
          • E eddieangel

            I have a database that houses (amongst other things) data on defects and repairs, car repairs as an example. The way this db breaks down is this: Table Defect holds the information about the defect (Damaged bumper, broken windshield, w/e) Table Material holds information about materials used to repair (ex: new bumper) Table Equipment holds information about equipment used to repair (ex: buffer) Table Trade holds information about who is responsible for the repair and their wage (ex: mechanic) There are three many to many tables that join defect with trade/eq/mat and have a quantity. Long story short, I have a Scalar value UDF that returns the value of all of these costs together divided by a value in the defect table. I decided to try to simplify it into a query (The simplification discussion is a TSQL issue outside the scope of this post) and in doing so had to ask myself, does this belong as a SQL query or should I write it as an additional property of the model in c#? I could just as easily write a partial class that handles all of this craziness on the web server side. Is this all pedantic? Is it really a question of db server performance vs web server performance? I have read that scalar value functions in TSQL can be low performing due to threading issues, which is what sparked this thing in the first place. Thoughts? Opinions? Cheers, --EA

            Kornfeld Eliyahu PeterK Offline
            Kornfeld Eliyahu PeterK Offline
            Kornfeld Eliyahu Peter
            wrote on last edited by
            #5

            Hi, From my experience SQL is extremely powerful on data manipulation, and if I understood it right that what you doing here... I see no reason (maybe conformance :)) to do it in C#, not even in a hosted CLR...

            I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is (V).

            "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

            1 Reply Last reply
            0
            • M Mycroft Holmes

              I would be asking myself where you are consuming the result, if it is ONLY in your UI then doing the calc in the model is valid. If however you intend to use it in reports then the database should be used. You may also consider storing the result somewhere if it is used in reports. Recalcing it every time you run a report may not be the best option.

              Never underestimate the power of human stupidity RAH

              E Offline
              E Offline
              eddieangel
              wrote on last edited by
              #6

              The question of storing the calculated field has been on my mind actually. In theory the calculation could happen one time (Only when the record is changed) and then stored. Otherwise it happens 1500 times (The number of records roughly) every time someone needs to access the data. I think you are right that recalcing is not the best option, in spite of the fact that it has been done that way for a long time. Thanks for the input, going to move it DB side, the amount of extra storage (1 field, 1500 to 3000 records) is negligible compared to the processing benefits. Cheers, --EA

              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