feat: Be careful with NULL
parent
fd56e04621
commit
c83c2bf60a
|
@ -1,3 +1,14 @@
|
|||
<!doctype html><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><link rel=preconnect href=https://fonts.googleapis.com><link rel=preconnect href=https://fonts.gstatic.com crossorigin><link href="https://fonts.googleapis.com/css2?family=Recursive:wght,CASL,MONO@300..800,0..1,0..1&display=swap" rel=stylesheet><link rel=stylesheet href=styles.css><a href=index>Index</a><h1>SQL</h1><p>Previously in my fresher time, I rarely write SQL, I always use ORM to wrap SQL.<br>But time past and too much abstraction bites me.<br>So I decide to only write SQL from now, no more ORM for me.<br>But if there is any cool ORM for Go, I guess I try.<p>This guide is not kind of guide which cover all cases.<br>Just my little tricks when I work with SQL.<h1>Stay away from database timestamp</h1><p>Stay away from all kind of timestamp, timestamp of MySQL or any database.<br>Just use int64 then pass the timestamp in service layer not database layer.<p>Why? Because time and date and location are too much complex to handle.<br>In my business, I use timestamp in milliseconds.<br>Then I save timestamp as int64 value to database.<br>Each time I get timestamp from database, I parse to time struct in Go with location or format I want.<br>No more hassle!<h1>Use index</h1><p>You should use index for faster query, but not too much.<br>Don't create index for every fields in table.<br>Choose wisely!<p>For example, create index in MySQL:<pre><code class=language-sql>CREATE INDEX `idx_timestamp`
|
||||
<!doctype html><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><link rel=preconnect href=https://fonts.googleapis.com><link rel=preconnect href=https://fonts.gstatic.com crossorigin><link href="https://fonts.googleapis.com/css2?family=Recursive:wght,CASL,MONO@300..800,0..1,0..1&display=swap" rel=stylesheet><link rel=stylesheet href=styles.css><a href=index>Index</a><h1>SQL</h1><p>Previously in my fresher time, I rarely write SQL, I always use ORM to wrap SQL.<br>But time past and too much abstraction bites me.<br>So I decide to only write SQL from now, no more ORM for me.<br>But if there is any cool ORM for Go, I guess I try.<p>This guide is not kind of guide which cover all cases.<br>Just my little tricks when I work with SQL.<h1>Stay away from database unique id</h1><p>Use UUID instead.<h1>Stay away from database timestamp</h1><p>Stay away from all kind of timestamp, timestamp of MySQL or any database.<br>Just use int64 then pass the timestamp in service layer not database layer.<p>Why? Because time and date and location are too much complex to handle.<br>In my business, I use timestamp in milliseconds.<br>Then I save timestamp as int64 value to database.<br>Each time I get timestamp from database, I parse to time struct in Go with location or format I want.<br>No more hassle!<h1>Use index</h1><p>You should use index for faster query, but not too much.<br>Don't create index for every fields in table.<br>Choose wisely!<p>For example, create index in MySQL:<pre><code class=language-sql>CREATE INDEX `idx_timestamp`
|
||||
ON `user_upload` (`timestamp`);
|
||||
</code></pre><h1>Be careful with NULL</h1><p>If compare with field which can be NULL, remember to check NULL for safety.<pre><code class=language-sql>-- field_something can be NULL
|
||||
|
||||
-- Don't
|
||||
SELECT *
|
||||
FROM table
|
||||
WHERE field_something != 1
|
||||
|
||||
-- Do
|
||||
SELECT *
|
||||
FROM table
|
||||
WHERE (field_something IS NULL OR field_something != 1)
|
||||
</code></pre><h1>Thanks</h1><ul><li><a href=https://use-the-index-luke.com/>Use The Index, Luke</a><li><a href=https://www.foxhound.systems/blog/essential-elements-of-high-performance-sql-indexes/>Essential elements of high performance applications: SQL indexes</a><li><a href=https://architecturenotes.co/things-you-should-know-about-databases/>Things You Should Know About Databases</a></ul><a href=mailto:hauvipapro+posts@gmail.com>Feel free to ask me via email</a>
|
|
@ -8,6 +8,10 @@ But if there is any cool ORM for Go, I guess I try.
|
|||
This guide is not kind of guide which cover all cases.
|
||||
Just my little tricks when I work with SQL.
|
||||
|
||||
# Stay away from database unique id
|
||||
|
||||
Use UUID instead.
|
||||
|
||||
# Stay away from database timestamp
|
||||
|
||||
Stay away from all kind of timestamp, timestamp of MySQL or any database.
|
||||
|
@ -32,6 +36,24 @@ CREATE INDEX `idx_timestamp`
|
|||
ON `user_upload` (`timestamp`);
|
||||
```
|
||||
|
||||
# Be careful with NULL
|
||||
|
||||
If compare with field which can be NULL, remember to check NULL for safety.
|
||||
|
||||
```sql
|
||||
-- field_something can be NULL
|
||||
|
||||
-- Don't
|
||||
SELECT *
|
||||
FROM table
|
||||
WHERE field_something != 1
|
||||
|
||||
-- Do
|
||||
SELECT *
|
||||
FROM table
|
||||
WHERE (field_something IS NULL OR field_something != 1)
|
||||
```
|
||||
|
||||
# Thanks
|
||||
|
||||
- [Use The Index, Luke](https://use-the-index-luke.com/)
|
||||
|
|
Loading…
Reference in New Issue