Mysql table with composite index but not primary key

后端 未结 2 1827
时光说笑
时光说笑 2020-12-06 15:30

I need a table to store some ratings, in this table I have a composite index (user_id, post_id) and other column to identify different rating system.

<
2条回答
  •  长情又很酷
    2020-12-06 16:16

    The missing of PRIMARY KEY may cause performance problem?

    Yes in InnoDB for sure, as InnoDB will use a algorithm to create it's own "ROWID", Which is defined in dict0boot.ic

    Returns a new row id.
    @return the new id */
    UNIV_INLINE
    row_id_t
    dict_sys_get_new_row_id(void)
    /*=========================*/
    {
        row_id_t    id;
    
        mutex_enter(&(dict_sys->mutex)); 
    
        id = dict_sys->row_id;
    
        if (0 == (id % DICT_HDR_ROW_ID_WRITE_MARGIN)) {
    
            dict_hdr_flush_row_id();
        }
    
        dict_sys->row_id++;
    
        mutex_exit(&(dict_sys->mutex));
    
        return(id);
    }
    

    The main problem in that code is mutex_enter(&(dict_sys->mutex)); which blocks others threads from accessing if one thread is already running this code. Meaning it will table lock the same as MyISAM would.

    % may take a few nanoseconds. That is insignificant compared to everything else. Anyway #define DICT_HDR_ROW_ID_WRITE_MARGIN 256

    Indeed yes Rick James this is indeed insignificant compared to what was mentioned above. The C/C++ compiler would micro optimize it more to to get even more performance out off it by making the CPU instructions lighter.
    Still the main performance concern is mentioned above..

    Also the modulo operator (%) is a CPU heavy instruction.
    But depening on the C/C++ compiler (and/or configuration options) if might be optimized if DICT_HDR_ROW_ID_WRITE_MARGIN is a power of two.
    Like (0 == (id & (DICT_HDR_ROW_ID_WRITE_MARGIN - 1))) as bitmasking is much faster, i believe DICT_HDR_ROW_ID_WRITE_MARGIN indeed had a number which is a power of 2

提交回复
热议问题