简体   繁体   中英

How to index database?

This is killing me - everybody say what it is but noone points to a guide or teach the basics.

  1. Is it something that is better done from the start or can you index it as easily if your loading times are getting longer?

  2. Has anyone found any good starting point for someone who's not a pro in databases? (I mean indexing starting point and don't worry, I know the basics of databases) Main rules, good practise etc.

Im not here to ask you to write a huge tutorial but if you're really, really bored - go ahead. :)

Im using Wordpress if that's important to know. Yes, I know that WP uses very basic indexing but if it's something good to start with from the beginning, I can't see a reason why not to.


  1. It's barely related but I also didn't find answer online. I can guess the answer but Im not 100% sure - what's more efficient way to store data with same key: in array or separate rows (separate ids but same keys)? There's usually maximum of 20 items per post & the number of posts could be in thousands in future. Which would be a better solution?

Different rows, ids & values BUT same key

id | key |values|
--------------------
25 | Bob | 3455 |
--------------------
24 | Bob | 1654 |
--------------------
23 | Bob | 8432 |

Same row, id & key BUT value is serialized array

id | key |      values      |
------------------------------
23 | Bob | serialized array |
------------------------------

If you want a quick rule of thumb, index any columns in a table that you will be using to lookup rows. For example, I may have a table as follows:

id| Name| date     |
--------------------
0 | Bob | 11.12.16 |
--------------------
1 | John| 15.12.16 |
--------------------
2 | Tim | 19.12.16 |

So obviously your ID is your primary index, but lets say you have a page that will SORT the whole table by DATE, well you would add date as an index.

Basically, indexes make it a lot faster for the engine to find specific records or order them by a specific column. They do a lot more, but when I am designing sites for myself or little tools for the office at work, I usually just go by that.

Large corporate tables can have thousands of indexes and even more relations between tables, but usually for us small peasant folk, what I said should be enough.

You're asking a really complicated question. But the tl;dr; A database index is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and storage space to maintain the index data structure.

more detailed info is already provided in the thorough answer here: How does database indexing work?

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