A quick project hosting all the codes I Wrote while writting the Blog about Dbq.
To Run the Project as is:
- Setup up a Database (call it whatever you want)
- Create a table called store in your database
CREATE TABLE store ( id INT PRIMARY KEY AUTO_INCREMENT, product VARCHAR(120) NOT NULL, quantity INT(10) NOT NULL, price DECIMAL NOT NULL, available BOOL, timing DATETIME NOT NULL);
- copy the config-example.json file to a config.json file in the same directory and fill in your db connection details.
- You can now Run codes in the project
main.go file is the Entry point of the project and it runs all the functions created in different files.
Before running go run main.go
in you terminal you would have to uncomment any of the function you don't want to execute.
The file naming of the other files are self explanatory.
- insert_withdbq.go: Contains Sql Database Insert Code snippet that use Dbq for db insert.
- insert_withoutdbq.go: Contains Sql Database Insert Code snippet that does not use Dbq for db insert.
- query_withdbq.go: Contains Sql Database Query Code snippets that use Dbq for db query.
- query_withoutdbq.go: Contains Sql Database Query Code snippets that does not use Dbq for db query.
For the sake of Benchmarking to compare the differences between when Query uses Dbq and does not use it, I wrote a DbScript which populates a database with up 100000 random name data (ofcourse you can increase or decrease the number if you want to investigate further).
To Run the script:
- Setup up a Database (call it whatever you want)
- Create a table called benchmark in your database
CREATE TABLE benchmark(id int primary key auto_increment not null, name varchar(255) not null, timestamp timestamp not null);
- cd into the dbScript directory
- make sure your db connection details are already set in a config.json file
- run the
go run main.go
command Just wait and your table will be populated with up to 100000 rows at 50hops of 2000 data insert per hop using DBQ
In the dbq-blog_test file, I wrote about five benchmark tests
To Run the Benchmark Test:
- make sure you are in the root directory
- make sure you have already set up benchmark Table and populated it with the script as directed above.
- make sure your db connection details are already set in a config.json file
- Run
go test -bench .
(This runs all the benchmark tests). Go to Go's Documentation to see other options available for the benchmark command (e.ggo test -bench . -benchtime=10x
)
goarch: amd64
pkg: dbq-blog
BenchmarkSingleRowQueryWithoutDBQ-2 2002 530847 ns/op
BenchmarkMultipleRowsQueryWithoutDBQ-2 1 1235999600 ns/op
BenchmarkSingleRowQueryWithDBQ-2 1916 595392 ns/op
BenchmarkMultipleRowsQueryWithDBQ-2 1 8387918970 ns/op
BenchmarkMultipleRowsQueryWithDBQNoTimeParse-2 1 6083932760 ns/op
PASS
ok dbq-blog 18.119s
Please, Do Note that: Speed is not the only Factor to be considered. Although the Speed measurement can help guide your decision to use Dbq in any project or not but the NUMBER 1 reason to use dbq is for convenience and productivity - not for speed.
This of course is a good task for beginners. By the time you go through this project and understand it well enough you can exercise yourself by adapting it to PostgreSQL. DBQ is compatible for both MySQL and PostgreSQL.
I would really love to learn from your PR. :D