feat: add sql (wip)

main
sudo pacman -Syu 2022-07-31 23:42:53 +07:00
parent a85a0e7d60
commit 7a86803a49
No known key found for this signature in database
GPG Key ID: D6CB5C6C567C47B0
2 changed files with 42 additions and 0 deletions

3
docs/2022-07-31-sql.html Normal file
View File

@ -0,0 +1,3 @@
<!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`
ON `user_upload` (`timestamp`);
</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>

39
posts/2022-07-31-sql.md Normal file
View File

@ -0,0 +1,39 @@
# SQL
Previously in my fresher time, I rarely write SQL, I always use ORM to wrap SQL.
But time past and too much abstraction bites me.
So I decide to only write SQL from now, no more ORM for me.
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 timestamp
Stay away from all kind of timestamp, timestamp of MySQL or any database.
Just use int64 then pass the timestamp in service layer not database layer.
Why? Because time and date and location are too much complex to handle.
In my business, I use timestamp in milliseconds.
Then I save timestamp as int64 value to database.
Each time I get timestamp from database, I parse to time struct in Go with location or format I want.
No more hassle!
# Use index
You should use index for faster query, but not too much.
Don't create index for every fields in table.
Choose wisely!
For example, create index in MySQL:
```sql
CREATE INDEX `idx_timestamp`
ON `user_upload` (`timestamp`);
```
# Thanks
- [Use The Index, Luke](https://use-the-index-luke.com/)
- [Essential elements of high performance applications: SQL indexes](https://www.foxhound.systems/blog/essential-elements-of-high-performance-sql-indexes/)
- [Things You Should Know About Databases](https://architecturenotes.co/things-you-should-know-about-databases/)