Results 1 to 10 of 10

Thread: tinyint vs NULL

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    tinyint vs NULL

    A lazy google search and I cannot find anything.
    I wanted to ask if there is any benefit on speed using a value (like -1) on a tinyint vs NULL.
    MSSQL server.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: tinyint vs NULL

    There shouldn't be. If there is, it's miniscule. Especially if the field is indexed (assuming you're looking for is null in a where clause).

    That said ... why do people try to avoid the use of NULL? NULL means value unknown. People complain and rail against using "magic numbers" in code, and yet using -1 in the database for an "unknown" value is the same thing - a magic number.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: tinyint vs NULL

    We are parsing bank extrait's. I'm not sure if the values should be null as it supposedly must have a value, p.e. a null card number is not really null but an error.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: tinyint vs NULL

    That's different from the scenario in the original post... and if it's an error, then it should be rejected and shouldn't have data made up to get it to conform.
    But what ever...


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: tinyint vs NULL

    If you are parsing bank extracts why are you asking about speed?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: tinyint vs NULL

    First we are parsing then process. Insert is easy with a bulk insert the rest currently,,,not sure.
    I'm not responsible on how the extract are given, I just have to process them.
    One thing for example that comes in mind is that if I use NULL's then a count for errors would have use ISNULL that to my understanding is not sargable. To be noted I'm not actually sure how we are going on about so I'm browsing choices. If it was that bad that I asked for speed then ignore me.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: tinyint vs NULL

    The speed issue relative to NULLS is all about how it "tracks" which fields are null.

    In a table without any NULL's (in ANY and ALL field definitions), for each row, there will just be buckets for each field, with a value stored within each bucket.

    If a table allows NULLS, then an additional "bit pattern" is stored with each row. Each of the bits corresponds to an ordinal "field" within the row. The bit values are checked prior to accessing each NULLABLE field, used to determine first if the field is NULL.

    This certainly seems negligible. But if you think of the actual I/O and memory processing, moving a chunk of data is a simple operation. Compared to checking a bit, then either moving a value or "creating" the NULL data "representation" in memory - that is a lot more. Do this a million million times, while reporting on a table with millions of rows, this can be costly.

    I use NULL for mostly DATE FIELDS in my tables. The proper normal-form way to handle DATES without NULL's usually requires creating a child table, and if a date is not known, that row is simply not created. That is cool concept for Ivory tower consideration, out in the fields (pun intended) not so much.
    Last edited by szlamany; Apr 16th, 2023 at 05:45 AM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: tinyint vs NULL

    Quote Originally Posted by szlamany View Post
    The speed issue relative to NULLS is all about how it "tracks" which fields are null.

    In a table without any NULL's (in ANY and ALL field definitions), for each row, there will just be buckets for each field, with a value stored within each bucket.

    If a table allows NULLS, then an additional "bit pattern" is stored with each row. Each of the bits corresponds to an ordinal "field" within the row. The bit values are checked prior to accessing each NULLABLE field, used to determine first if the field is NULL.

    This certainly seems negligible. But if you think of the actual I/O and memory processing, moving a chuck of data is a simple operation. Compared to checking a bit, then either moving a value or "creating" the NULL data "representation" in memory - that is a lot more. Do this a million million times, while reporting on a table with millions of rows, this can be costly.

    I use NULL for mostly DATE FIELDS in my tables. The proper normal-form way to handle DATES without NULL's usually requires creating a child table, and if a date is not known, that row is simply not created. That is cool concept for Ivory tower consideration, out in the fields (pun intended) not so much.
    Don't forget COUNT in regards to NULL:
    NULLs are NOT counted
    I actually do use this attribute of NULLs in my queries doing COUNTs on "how many times did we ship this item to this client" by counting the ShippingNote-Numbers (which are distinct), but the ShippingNote-Number is a nullable field in our dbms, since the ShippingNote-Number is created/applied AFTER the packing is done (as in: Number is created when die ShippingNote is printed).
    And in our company there can be a whole week between starting/preparing shipment, and the actual shipment (when the item leaves through our doors)

    As to "ISNULL is not sargable":
    That's because ISNULL is a Function, and is executed for each row.
    "WHERE SomeThing IS [NOT] NULL" otoh IS sargable
    Last edited by Zvoni; Apr 11th, 2023 at 08:29 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  9. #9

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: tinyint vs NULL

    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  10. #10
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: tinyint vs NULL

    Quote Originally Posted by sapator View Post
    We are parsing bank extrait's. I'm not sure if the values should be null as it supposedly must have a value, p.e. a null card number is not really null but an error.
    But a death date not being NULL doesn't make sense either.
    Please remember next time...elections matter!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width