<!doctype html><metacharset=utf-8><metaname=viewportcontent="width=device-width,initial-scale=1"><linkrel=stylesheethref=https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.1.0/github-markdown-dark.css><style>.markdown-body{box-sizing:border-box;min-width:200px;max-width:980px;margin:0auto;padding:45px}@media(max-width:767px){.markdown-body{padding:15px}}</style><bodyclass=markdown-body><ahref=index>Index</a><h1><aid=user-content-sqlclass=anchoraria-hidden=truehref=#sql><spanaria-hidden=trueclass="octicon octicon-link"></span></a>SQL</h1><p>Previously in my fresher software developer 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 as much as possible, no more ORM for me.
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.
Just my little tricks when I work with SQL.<h2><aid=user-content-stay-away-from-database-unique-idclass=anchoraria-hidden=truehref=#stay-away-from-database-unique-id><spanaria-hidden=trueclass="octicon octicon-link"></span></a>Stay away from database unique id</h2><p>Use UUID instead.
If you can, and you should, choose UUID type which can be sortable.<h2><aid=user-content-stay-away-from-database-timestampclass=anchoraria-hidden=truehref=#stay-away-from-database-timestamp><spanaria-hidden=trueclass="octicon octicon-link"></span></a>Stay away from database timestamp</h2><p>Stay away from all kind of database timestamp (MySQL timestmap, SQLite timestamp, ...)
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.
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!<p>It looks like this:<divclass="highlight highlight-text-adblock"><pre>[Business] time, data -> convert to unix timestamp milliseconds -> [Database] int64</pre></div><h2><aid=user-content-use-indexclass=anchoraria-hidden=truehref=#use-index><spanaria-hidden=trueclass="octicon octicon-link"></span></a>Use index!!!</h2><p>You should use index for faster query, but not too much.
Don't create index for every fields in table.
Choose wisely!<p>For example, create index in MySQL:<divclass="highlight highlight-source-sql"><pre><spanclass=pl-k>CREATE</span><spanclass=pl-k>INDEX</span> `<spanclass=pl-en>idx_timestamp</span>`
<spanclass=pl-k>ON</span><spanclass=pl-s><spanclass=pl-pds>`</span>user_upload<spanclass=pl-pds>`</span></span> (<spanclass=pl-s><spanclass=pl-pds>`</span>timestamp<spanclass=pl-pds>`</span></span>);</pre></div><h2><aid=user-content-be-careful-with-nullclass=anchoraria-hidden=truehref=#be-careful-with-null><spanaria-hidden=trueclass="octicon octicon-link"></span></a>Be careful with NULL</h2><p>If compare with field which can be NULL, remember to check NULL for safety.<divclass="highlight highlight-source-sql"><pre><spanclass=pl-c><spanclass=pl-c>--</span> field_something can be NULL</span>
<spanclass=pl-k>WHERE</span> (field_something IS <spanclass=pl-k>NULL</span><spanclass=pl-k>OR</span> field_something <spanclass=pl-k>!=</span><spanclass=pl-c1>1</span>)</pre></div><p>Need clarify why this happpen? Idk :(<h2><aid=user-content-varchar-or-textclass=anchoraria-hidden=truehref=#varchar-or-text><spanaria-hidden=trueclass="octicon octicon-link"></span></a>
<code>VARCHAR</code> or <code>TEXT</code></h2><p>Prefer <code>VARCHAR</code> if you need to query and of course use index, 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.<h2><aid=user-content-be-super-careful-when-migrate-update-database-on-production-and-onlineclass=anchoraria-hidden=truehref=#be-super-careful-when-migrate-update-database-on-production-and-online><spanaria-hidden=trueclass="octicon octicon-link"></span></a>Be super careful when migrate, update database on production and online!!!</h2><p>Plase read docs about online ddl operations before do anything online (keep database running the same time update it, for example create index, ...)<ul><li><ahref=https://dev.mysql.com/doc/refman/5.7/en/innodb-online-ddl-operations.htmlrel=nofollow>For MySQL 5.7</a>, <ahref=https://dev.mysql.com/doc/refman/5.7/en/innodb-online-ddl-limitations.htmlrel=nofollow>Limitations</a><li><ahref=https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-operations.htmlrel=nofollow>For MySQL 8.0</a>, <ahref=https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-limitations.htmlrel=nofollow>Limitations</a></ul><h2><aid=user-content-toolsclass=anchoraria-hidden=truehref=#tools><spanaria-hidden=trueclass="octicon octicon-link"></span></a>Tools</h2><ul><li>Use <ahref=https://github.com/sqlfluff/sqlfluff>sqlfluff/sqlfluff</a> to check your SQL.<li>Use <ahref=https://github.com/k1LoW/tbls>k1LoW/tbls</a> to grasp your database reality :)</ul><h2><aid=user-content-thanksclass=anchoraria-hidden=truehref=#thanks><spanaria-hidden=trueclass="octicon octicon-link"></span></a>Thanks</h2><ul><li><ahref=https://use-the-index-luke.com/rel=nofollow>Use The Index, Luke</a><li><ahref=https://www.foxhound.systems/blog/essential-elements-of-high-performance-sql-indexes/rel=nofollow>Essential elements of high performance applications: SQL indexes</a><li><ahref=https://architecturenotes.co/things-you-should-know-about-databases/rel=nofollow>Things You Should Know About Databases</a></ul><ahref=mailto:hauvipapro+posts@gmail.com>Feel free to ask me via email</a>