简体   繁体   中英

Columns with varchar vs float in sql server for empty values

In my table there will be 50 columns with float datatype, They may be empty ( ie null) , partially empty or completely full for each row.

What is the ideal design for this case

  1. To have float as column datatype and null for empty values
  2. To have float as column datatype and -1.0 for empty values
  3. To have varchar as datatype and null for empty values.

I have come across the fact that if columns are of variable length then the null will not occupy any space. In that case 3rd option would be desirable but am afraid of the performance due to string comparisons for search queries.

On considering performance and disk usage , which is the ideal solution for my table.

EDIT : Based on the suggestions I am dropping of the 2nd and 3rd choice. With respect to the first choice will it be better If I create 50 seperate tables for each column and join to the main table with a primary key . Such that there won't be empty spaces and aslo I can use decimal/long as datatype. Will this solution hold good ? Based on the suggestions I am dropping of the 2nd and 3rd choice. With respect to the first choice will it be better If I create 50 seperate tables for each column and join to the main table with a primary key . Such that there won't be empty spaces and aslo I can use decimal/long as datatype. Will this solution hold good ?

Use the most appropriate datatype. If the columns are floats, then use floats. On the limited info, I would probably go with option 1. 3 would be a terrible idea.

The database offers you many datatypes. So you can take the most appropriate.

Of course you can store a decimal number in a varchar, but why would you ever do such a thing? You would have to define the format you store the number in and then keep this in mind every time you use it. Everytime you want to calculate with it, you will have to convert. Everytime you want to show it, you would have to convert your decimal seperator to the client's decimal seperator. Instead of storing four or eight bytes for the number you would store two handling bytes plus bytes per character. Many, many cons, no pros.

-1.0 instead of null? What the hell is this supposed to be good for? So to have to replace SUM(col) with SUM(CASE WHEN col = -1.0 THEN NULL ELSE col END) everytime? Stay away from this. Use, what the dbms offers you. NULL is the thing to use when no value is given.

So solution 1 is the only one that makes sense. However, are you sure you want to use FLOAT? Are your numbers that big? Otherwise I see no good in using an approximate type instead of storing the number precisely with the DECIMAL data type.

Use the datatype of the value required for the cell. If the values are always numeric use FLOAT or another appropriate numeric datatype. A search and comparison of numeric values alone is likely going to be much faster than comparison of textual matching with type conversions.

If storage is of major concern consider the option to use SPARSE columns in your table. Its optimized for null value rows. NULL values in SPARSE columns negates its storage expense. http://msdn.microsoft.com/en-us/library/cc280604.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM