Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:31:18+00:00 2026-05-28T23:31:18+00:00

I am creating reusable Data Access Layer for SQL Server for some projects. I

  • 0

I am creating reusable Data Access Layer for SQL Server for some projects. I have a problem with finding a proper way to pass .NETs decimal as a parameter to query. Most obvious way:

cmd.Parameters.AddWithValue("@P0", parameterValue);

works almost good. The value is stored properly in DB, but SqlClient infers exact type from actual value, so when application passes 12345.678 it declares @P0 as NUMERIC(8,3). When exactly same query is used with other value, that value’s precision and scale is declared. For a reason yet not known to me, from SQL Server those queries are not equal and each of them gets separate entry in sys.dm_exec_cached_plans. There is exactly same problem with string – for each length there is a different query plan.

Some of our applications write quite a few records per minute, each with 5-10 decimal values with wildly varying ranges (some sensor data, some currency values). Almost each INSERT gets it’s own query plan and after a days there are more than 100k versions of the same query plan cached, taking well over few GBs of RAM.

My idea to deal with is to coalesce all possible types to a few arbitrary chosen types. For decimal, when precision is lower than 20 I set it to 20. When precision is greater than 20, I set it as smallest value divisible by 4 greater than actual precision (e.g. 24, 28, 32, 36). Similar goes for scale (minimum 2, then steps by 2) and strings (minumum 128, then powers of 2). This way seems to keep the problem under control – I’ve seen around 100 variants of plans for each query instead of 100k and SQL Server uses memory for buffer pool, not for some useless plans.

Is there any way this approach could go wrong? Are there better ways to keep query plans cache under control when using SqlClient and queries loaded with decimals?

List of things I can’t do:

  • Use stored procedures – because purpose of this DAL is to create abstract layer for many DBMSes (SQL Server, MS Access, Firebird, Oracle at the moment).
  • Force my fellow teammates to somehow pass actual type with each variable – because that would be cruel.
  • Trash this DAL, use plain old DbProviderFactory and create parameters like it’s 1990 – because this DAL also handles query creation for SQL dialects, DB structure creation, etc.
  • Trash this DAL and use “proper” DAO like NHibernate – because it was Not Invented Here.
  • Take this question to dba.stackexchange.com – because it’s a problem with SqlClient and the way I use it, not with SQL Server per se.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:31:19+00:00Added an answer on May 28, 2026 at 11:31 pm

    You correctly identified the key issue here as letting SQL driver decide the sizes, scales, and precisions on your parameters for you. Your idea of reducing the number of query plans by limiting precisions / scales / sizes of parameters also sounds correct. For strings, you can set the size to the largest value that you expect: it should work correctly when your query compares parameter strings with large size limits to varchars of smaller size. Picking just a few precisions for decimals also sounds right. See if you can get it down to just one pair of precision/scale: it may be possible (it worked out very well for our projects at work). If you manage to do it, you’d be able to go to a single query plan per SQL query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data/helper classes defined which I use in several projects. These data
I have been tasked with creating a reusable process for our Finance Dept to
I'm developing a reusable library and have been creating abstract classes, so the client
I am creating a C# library with some reusable code and was trying to
Main question: Is there a better way to accomplish creating a reusable control? So
I am creating a very long table with images and some text data. I
I am creating some reusable objects in php, and I wanted to know whats
Are there any resources with information creating a self contained, reusable module? I need
Creating a server-side socket will fail if I'm trying to use the same port
I was working on creating a reusable custom control (not a user control) in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.