ARTS Week4
by
Background: ARTS activity
Algorithm
Review
How to work optimally with relational databases
Data in SQL database are arranged as tuples which are the gather of data attributes.
Here are some ways the post recommends to optimally use SQL databases:
-
Put Index on important attributes.
In essecence, this helps the db engine to avoid scanning the whole table and achieve speed-up on queries. Both composite indices or single index are available. “Order By” can also index to speed up the sorting process.
-
Try to avoid the select on un-indexed fields and select the required field.
e.g “select * from …”. This makes the engine go back to the whole table to look for all of the attributes since the index doesn’t hold the total information. Tip: use explain statement to get a detailed searching schema about a select statement.
-
Use “Limit” and “Offset” to skip some scanning.
The above recommendations lie on the very idea that skip the scanning of the whole table can speed up the query.
-
Partition the table.
When it comes to selecting the un-indexed fields, db needs to look back at the main table to get the data for other fields. As such, minimizing the size of a table and partition it into several smaller tables improve the query performance.
-
Sharding the whole data set.
Partitioning the table decreases the size of a single table but it takes effect on one single machine and this optimization is limited. However sharding can horizontally split the data set to different machines and achieve scalability. E.g split the data into hot and cold parts.
Tools:
-
Use these to see the execution time of a statement:
Enable the profiling.
set profiling=1;
Show the profiling result.
show profiles;
-
Explain a statement.
Tip
The state pattern allows an object to alter its behavior when its internal state changes. It is easy to implement state pattern when a language regards function as an object, which means it supports functional programming style. Since the behavior of an object is defined by the function itself, when it comes to the change of the internal state, just make the object point to another function to change its behavior. For example, when we need to write a double number parser, for simplicity, we will have states like START, INTEGER, DOT, DECIMAL, END and ERROR. Each state is represented by a function.
First, we can define our parser data structure and it holds an interal state. It takes a string and outputs a double number if the string is in the correct pattern otherwise output an error.
The parser is just a simple loop accepting the change of state as it consumes the input and how the state changes is decided by the state itself.
Second encapsulate the state as an interface and define its own behavior.
A similar implementation with Java looks like this:
Comparing the two implementations, we find out that the first version provides a more clear parser since the control of the state is managed by the states whereas the second implementation maintain a central to manage the state transition. When it comes to the more complicated state machines, the decouple of the parser and state management is good for maintaining the right state by given input and easier to debug. And this is one of the fascinating feature of functional programming.
Share
The idea of the tip section comes from the inspiring talk of Lexical Scanning by Rob Pike
tags: