Posts

Showing posts with the label Database

[Database] Simple SQL vs NoSQL

1. Schema SQL stores data in tables. NoSQL stores in different methods (key/value, document, columnar and graph) 2. Querying SQL databases uses regular SQL statements. NoSQL uses unQL (Unstructured Query Language) 3. Scalability SQL databases are vertically scalable (beef the same server hardware). NoSQL databases are horizontally scalable (add more servers) 4. Reliability SQL databases are ACID friendly, NoSQL sacrifices ACID property for performance reasons. 4.1) What is ACID? Set of properties of database transactions to guarantee validity even in the event of errors, power failures.etc. Atomicity Consistency Isolation Durability 4.1.1) Atomicity Transactions are often composed of multiple statements.  Atomicity guarantees each transaction is treated a single 'unit', which either succeeds completely or fails completely.  If any of the statements in a transaction fails to complete, the entire transaction fails and database is left unch...

[NoSql] 4 Types of NoSQL Database

1. Key-Value Stores Popular db: Redis, Dynamo 2. Document Databases 2.1) Features Data are stored as 'object' in some standard encoding, such as json or xml. Sub-class of key-value store. No schema (because this is NoSql) Key-Document Relationship The key is used to retrieve the document from the database. Query by content/metadata Beyond key-to-document lookup, database offers an API or query language that enables users to retrieve documents based on content/metadata. For example, you may want a query that retrieves all documents with a certain field set to a certain value. This is what uniquely differs from key-value stores. 2.2) Popular Databases MongoDB, CouchDB 3. Column Databases 3.1) Features Instead of storing data in rows, these databases store data by column. 3.2) When to use? Highly dependent on use case: During retrieval, does requested data have attributes that we don't always need? Good for Business Analytics o...

[Redis] Quick Start to Redis

Officially, Redis is not supported on Windows. There is, however, a 3.2.1 version of Redis that was ported to Windows by MSOpenTech. Here, I will show you how to get a redis instance running very quickly. If you want a more stable implementation, please read from https://redislabs.com/blog/redis-on-windows-8-1-and-previous-versions. 1. Download Redis MSOpenTech has support for 3.2.1 of Redis.  Download the zip from  https://github.com/MicrosoftArchive/redis/release 2. Running Redis Server + Cli Unzip the downloaded zip, and run redis-server.exe Then, run redis-cli.exe In the cli, you should enter and expect: 127.0.0.1:6379> get foo (nil) 127.0.0.1:6379> set foo 123 OK 127.0.0.1:6379> get foo "123" 3. Accessing Redis Server from Python and Ruby Script Python: 1. install redis (pip install redis) 2. Run script: import redis r = redis.Redis(host='localhost', port=6379, db=0) print("Setting bar to key, foo")...

[ORM] Eager Loading and N+1 Query Problem

What is N+1 query problem? When your code loads the children in a parent-child relationship via the parent, most ORM have lazy-loading enabled by default, so queries are issued for the parent record, and then one query for EACH child record. As you can expect, doing N + 1 queries instead of a single query will floor your database with queries; something to avoid.  Consider this code: #Articles model class Article < ActiveRecord::Base     belongs_to :author end  #Authors model class Author < ActiveRecord::Base     has_many :posts end If you then ran: #In our controller @recent_articles = Article.order(published_at: :desc).limit(5) #in our view file @recent_articles.each do |article|     Title: <%= article.title %>     Author:<%= article.author.name %> # <-- this will trigger N+1 query end You would send 6 (5+1) queries to the database. 1 to fetch 5 recent articles, and then 5 for their correspo...