Auto increment id column vs index VARCHAR columns












0















I'm wondering the differences or comparisons of having an Auto Increment int Id column or an indexed VARCHAR(32) column. Which one would be faster if I had a second table that references the first table by joining? Like in the second table, would I store the Auto Increment INT id or the indexed VARCHAR column? What are the advantages or disadvantages, if any?



Which would be the more common practice? Create Auto Increment id column for almost every table? Or we can simply index a column that has a unique 36-char ID (like in SugarCRM) so the other tables can just use that when doing joins? I hope I explained my question fairly clear.










share|improve this question














bumped to the homepage by Community 25 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Sounds like you have UUIDs. Use BINARY(16), not VARCHAR(36). If used as foreign key, it won't be as good as INT (4 bytes) or BIGINT (8 bytes) but still 16 is far better than 36.

    – yper-crazyhat-cubeᵀᴹ
    Jun 24 '16 at 10:26













  • Ok. Yea I remember now about the hexadecimal id's.

    – TurtleTread
    Jun 24 '16 at 10:51
















0















I'm wondering the differences or comparisons of having an Auto Increment int Id column or an indexed VARCHAR(32) column. Which one would be faster if I had a second table that references the first table by joining? Like in the second table, would I store the Auto Increment INT id or the indexed VARCHAR column? What are the advantages or disadvantages, if any?



Which would be the more common practice? Create Auto Increment id column for almost every table? Or we can simply index a column that has a unique 36-char ID (like in SugarCRM) so the other tables can just use that when doing joins? I hope I explained my question fairly clear.










share|improve this question














bumped to the homepage by Community 25 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Sounds like you have UUIDs. Use BINARY(16), not VARCHAR(36). If used as foreign key, it won't be as good as INT (4 bytes) or BIGINT (8 bytes) but still 16 is far better than 36.

    – yper-crazyhat-cubeᵀᴹ
    Jun 24 '16 at 10:26













  • Ok. Yea I remember now about the hexadecimal id's.

    – TurtleTread
    Jun 24 '16 at 10:51














0












0








0








I'm wondering the differences or comparisons of having an Auto Increment int Id column or an indexed VARCHAR(32) column. Which one would be faster if I had a second table that references the first table by joining? Like in the second table, would I store the Auto Increment INT id or the indexed VARCHAR column? What are the advantages or disadvantages, if any?



Which would be the more common practice? Create Auto Increment id column for almost every table? Or we can simply index a column that has a unique 36-char ID (like in SugarCRM) so the other tables can just use that when doing joins? I hope I explained my question fairly clear.










share|improve this question














I'm wondering the differences or comparisons of having an Auto Increment int Id column or an indexed VARCHAR(32) column. Which one would be faster if I had a second table that references the first table by joining? Like in the second table, would I store the Auto Increment INT id or the indexed VARCHAR column? What are the advantages or disadvantages, if any?



Which would be the more common practice? Create Auto Increment id column for almost every table? Or we can simply index a column that has a unique 36-char ID (like in SugarCRM) so the other tables can just use that when doing joins? I hope I explained my question fairly clear.







mysql index






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 24 '16 at 9:50









TurtleTreadTurtleTread

1012




1012





bumped to the homepage by Community 25 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 25 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Sounds like you have UUIDs. Use BINARY(16), not VARCHAR(36). If used as foreign key, it won't be as good as INT (4 bytes) or BIGINT (8 bytes) but still 16 is far better than 36.

    – yper-crazyhat-cubeᵀᴹ
    Jun 24 '16 at 10:26













  • Ok. Yea I remember now about the hexadecimal id's.

    – TurtleTread
    Jun 24 '16 at 10:51



















  • Sounds like you have UUIDs. Use BINARY(16), not VARCHAR(36). If used as foreign key, it won't be as good as INT (4 bytes) or BIGINT (8 bytes) but still 16 is far better than 36.

    – yper-crazyhat-cubeᵀᴹ
    Jun 24 '16 at 10:26













  • Ok. Yea I remember now about the hexadecimal id's.

    – TurtleTread
    Jun 24 '16 at 10:51

















Sounds like you have UUIDs. Use BINARY(16), not VARCHAR(36). If used as foreign key, it won't be as good as INT (4 bytes) or BIGINT (8 bytes) but still 16 is far better than 36.

– yper-crazyhat-cubeᵀᴹ
Jun 24 '16 at 10:26







Sounds like you have UUIDs. Use BINARY(16), not VARCHAR(36). If used as foreign key, it won't be as good as INT (4 bytes) or BIGINT (8 bytes) but still 16 is far better than 36.

– yper-crazyhat-cubeᵀᴹ
Jun 24 '16 at 10:26















Ok. Yea I remember now about the hexadecimal id's.

– TurtleTread
Jun 24 '16 at 10:51





Ok. Yea I remember now about the hexadecimal id's.

– TurtleTread
Jun 24 '16 at 10:51










1 Answer
1






active

oldest

votes


















0














UUIDs are terrible when the table becomes too large to be cached. (If your table will never get very big, you can ignore the rest of my rant.)



The problem comes with the randomness of UUIDs. You fetch one row; it's in one place. You fetch another row, it is extremely likely to be in some other block. This leads to lots of I/O.



In contrast, AUTO_INCREMENT ids are inserted in roughly chronological order. A bunch (say, 100) rows will land in one block before moving on to the next block. This makes INSERTs efficient. It often makes reads efficient, too -- because often you are looking for records of similar vintage -- such as with a date range.



Think of News articles. Most people look at the latest news; the older records collect dust in the cache until they are bumped out. So, the cache tends to keep the latest rows. With UUIDs, old and new articles share the same block.



More on UUIDs.






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "182"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f142135%2fauto-increment-id-column-vs-index-varchar-columns%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    UUIDs are terrible when the table becomes too large to be cached. (If your table will never get very big, you can ignore the rest of my rant.)



    The problem comes with the randomness of UUIDs. You fetch one row; it's in one place. You fetch another row, it is extremely likely to be in some other block. This leads to lots of I/O.



    In contrast, AUTO_INCREMENT ids are inserted in roughly chronological order. A bunch (say, 100) rows will land in one block before moving on to the next block. This makes INSERTs efficient. It often makes reads efficient, too -- because often you are looking for records of similar vintage -- such as with a date range.



    Think of News articles. Most people look at the latest news; the older records collect dust in the cache until they are bumped out. So, the cache tends to keep the latest rows. With UUIDs, old and new articles share the same block.



    More on UUIDs.






    share|improve this answer




























      0














      UUIDs are terrible when the table becomes too large to be cached. (If your table will never get very big, you can ignore the rest of my rant.)



      The problem comes with the randomness of UUIDs. You fetch one row; it's in one place. You fetch another row, it is extremely likely to be in some other block. This leads to lots of I/O.



      In contrast, AUTO_INCREMENT ids are inserted in roughly chronological order. A bunch (say, 100) rows will land in one block before moving on to the next block. This makes INSERTs efficient. It often makes reads efficient, too -- because often you are looking for records of similar vintage -- such as with a date range.



      Think of News articles. Most people look at the latest news; the older records collect dust in the cache until they are bumped out. So, the cache tends to keep the latest rows. With UUIDs, old and new articles share the same block.



      More on UUIDs.






      share|improve this answer


























        0












        0








        0







        UUIDs are terrible when the table becomes too large to be cached. (If your table will never get very big, you can ignore the rest of my rant.)



        The problem comes with the randomness of UUIDs. You fetch one row; it's in one place. You fetch another row, it is extremely likely to be in some other block. This leads to lots of I/O.



        In contrast, AUTO_INCREMENT ids are inserted in roughly chronological order. A bunch (say, 100) rows will land in one block before moving on to the next block. This makes INSERTs efficient. It often makes reads efficient, too -- because often you are looking for records of similar vintage -- such as with a date range.



        Think of News articles. Most people look at the latest news; the older records collect dust in the cache until they are bumped out. So, the cache tends to keep the latest rows. With UUIDs, old and new articles share the same block.



        More on UUIDs.






        share|improve this answer













        UUIDs are terrible when the table becomes too large to be cached. (If your table will never get very big, you can ignore the rest of my rant.)



        The problem comes with the randomness of UUIDs. You fetch one row; it's in one place. You fetch another row, it is extremely likely to be in some other block. This leads to lots of I/O.



        In contrast, AUTO_INCREMENT ids are inserted in roughly chronological order. A bunch (say, 100) rows will land in one block before moving on to the next block. This makes INSERTs efficient. It often makes reads efficient, too -- because often you are looking for records of similar vintage -- such as with a date range.



        Think of News articles. Most people look at the latest news; the older records collect dust in the cache until they are bumped out. So, the cache tends to keep the latest rows. With UUIDs, old and new articles share the same block.



        More on UUIDs.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 4 '16 at 1:03









        Rick JamesRick James

        41.2k22258




        41.2k22258






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Database Administrators Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f142135%2fauto-increment-id-column-vs-index-varchar-columns%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Ronny Ackermann

            Köttigit

            MySQL 8.0.15 starts normally but any connection hangs