From 7a86803a49c41339b8204c6f26c2fed0149edcbf Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Sun, 31 Jul 2022 23:42:53 +0700 Subject: [PATCH] feat: add sql (wip) --- docs/2022-07-31-sql.html | 3 +++ posts/2022-07-31-sql.md | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 docs/2022-07-31-sql.html create mode 100644 posts/2022-07-31-sql.md diff --git a/docs/2022-07-31-sql.html b/docs/2022-07-31-sql.html new file mode 100644 index 0000000..e61672f --- /dev/null +++ b/docs/2022-07-31-sql.html @@ -0,0 +1,3 @@ +Index

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:

CREATE INDEX `idx_timestamp`
+    ON `user_upload` (`timestamp`);
+

Thanks

Feel free to ask me via email \ No newline at end of file diff --git a/posts/2022-07-31-sql.md b/posts/2022-07-31-sql.md new file mode 100644 index 0000000..08647dc --- /dev/null +++ b/posts/2022-07-31-sql.md @@ -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/)