feat: morr about index

main
sudo pacman -Syu 2023-06-16 02:31:21 +07:00
parent 3e9eead44e
commit 5cffdc47c7
2 changed files with 44 additions and 4 deletions

View File

@ -126,8 +126,23 @@
</p>
<p>For example, create index in MySQL:</p>
<div class="highlight highlight-source-sql">
<pre><span class="pl-k">CREATE</span> <span class="pl-k">INDEX</span> `<span class="pl-en">idx_timestamp</span>`
<span class="pl-k">ON</span> <span class="pl-s"><span class="pl-pds">`</span>user_upload<span class="pl-pds">`</span></span> (<span class="pl-s"><span class="pl-pds">`</span>timestamp<span class="pl-pds">`</span></span>);</pre>
<pre><span class="pl-k">CREATE</span> <span class="pl-k">INDEX</span> <span class="pl-en">idx_user_id</span>
<span class="pl-k">ON</span> user_upload (user_id);</pre>
</div>
<p>
If create index inside <code>CREATE TABLE</code>,
<a href="https://stackoverflow.com/a/1401615" rel="nofollow"
>prefer <code>INDEX</code> to <code>KEY</code></a
>:
</p>
<div class="highlight highlight-source-sql">
<pre><span class="pl-k">CREATE</span> <span class="pl-k">TABLE</span> <span class="pl-en">user_upload</span>
(
id <span class="pl-k">int</span>(<span class="pl-c1">11</span>) <span class="pl-k">NOT NULL</span>,
user_id <span class="pl-k">int</span>(<span class="pl-c1">11</span>) <span class="pl-k">NULL</span> DEFAULT <span class="pl-k">NULL</span>,
<span class="pl-k">PRIMARY KEY</span> (id),
INDEX idx_user_id (user_id)
);</pre>
</div>
<p>Use <code>EXPLAIN</code> to check if index is used or not:</p>
<ul>
@ -188,6 +203,15 @@
and make sure size of value will never hit the limit. Prefer
<code>TEXT</code> if you don't care, just want to store something.
</p>
<h2>
<a id="user-content-limit" class="anchor" aria-hidden="true" href="#limit"
><span aria-hidden="true" class="octicon octicon-link"></span></a
><code>LIMIT</code>
</h2>
<p>
Prefer <code>LIMIT 10 OFFSET 5</code> to <code>LIMIT 5, 10</code> to avoid
misunderstanding.
</p>
<h2>
<a
id="user-content-be-super-careful-when-migrate-update-database-on-production-and-online"

View File

@ -49,8 +49,20 @@ Choose wisely!
For example, create index in MySQL:
```sql
CREATE INDEX `idx_timestamp`
ON `user_upload` (`timestamp`);
CREATE INDEX idx_user_id
ON user_upload (user_id);
```
If create index inside `CREATE TABLE`, [prefer `INDEX` to `KEY`](https://stackoverflow.com/a/1401615):
```sql
CREATE TABLE user_upload
(
id int(11) NOT NULL,
user_id int(11) NULL DEFAULT NULL,
PRIMARY KEY (id),
INDEX idx_user_id (user_id)
);
```
Use `EXPLAIN` to check if index is used or not:
@ -83,6 +95,10 @@ Need clarify why this happpen? Idk :(
Prefer `VARCHAR` if you need to query and of course use index, and make sure size of value will never hit the limit.
Prefer `TEXT` if you don't care, just want to store something.
## `LIMIT`
Prefer `LIMIT 10 OFFSET 5` to `LIMIT 5, 10` to avoid misunderstanding.
## Be super careful when migrate, update database on production and online!!!
Plase read docs about online ddl operations before do anything online (keep database running the same time update it, for example create index, ...)