简体   繁体   中英

What is the advantage of using composite primary key instead of single primary key in this context?

Cakephp v3 is now able to support composite primary key. http://book.cakephp.org/3.0/en/quickstart.html

One example given at the quick start guide;

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    email VARCHAR(255) NOT NULL,
    password VARCHAR(255) NOT NULL,
    created DATETIME,
    modified DATETIME
);

CREATE TABLE bookmarks (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    title VARCHAR(50),
    description TEXT,
    url TEXT,
    created DATETIME,
    modified DATETIME,
    FOREIGN KEY user_key (user_id) REFERENCES users(id)
);

CREATE TABLE tags (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255),
    created DATETIME,
    modified DATETIME,
    UNIQUE KEY (title)
);

CREATE TABLE bookmarks_tags (
    bookmark_id INT NOT NULL,
    tag_id INT NOT NULL,
    PRIMARY KEY (bookmark_id, tag_id),
    INDEX tag_idx (tag_id, bookmark_id),
    FOREIGN KEY tag_key(tag_id) REFERENCES tags(id),
    FOREIGN KEY bookmark_key(bookmark_id) REFERENCES bookmarks(id)
);

The Quick start guide mentions "You may have noticed that the bookmarks_tags table used a composite primary key. CakePHP supports composite primary keys almost everywhere, **making it easier to build multi-tenanted applications**."

What is meant by multi-tenanted applications? In this context, why is it better to use composite primary key versus single primary key?

In a nutshell: clustering .

InnoDB will automatically cluster (physically group together) bookmarks_tags rows with the same bookmark_id , making certain queries 1 extremely fast because DBMS doesn't have to "jump" all over the table to gather all the related rows.

There is no need for a surrogate key (auto-increment int) in this case, nor for the additional price you'd otherwise pay for its underlying index. For more thorough discussion on natural vs. surrogate keys, take a look here .

While I'm not familiar with Cakephp v3, I would guess that they simply mean that you can prefix the keys of all tables with a tenant ID and pay very little in performance terms due to clustering.


1 Such as: "find tags of given bookmark". And since you also have index on {tag_id, bookmark_id} , the opposite query: "find bookmarks of given tag" will also be fast.

Multi-tenanting is keeping several different organizations' data in the same database, but in a way that keeps their data logically separate from each other. This is a common approach for system providers who have many customers using one common application.

The significance of the comment in the CakePHP documentation that you cited is that they expect you to segregate multiple organizations' (tenants') data within a single set of tables by adding something like org_id to each table's primary key.

Of course, there are other reasons to use composite primary keys within the context of a single user system too, but obviously CakePHP had multi-tenant support in mind when they added this feature.

如果我们确实select * from bookmarks_tags那么将根据表bookmarks_tags bookmarks_id(first field)进行排序

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