Skip to content

Instantly share code, notes, and snippets.

View alekseyl's full-sized avatar
👨‍💻

Aleksey Leshchuk alekseyl

👨‍💻
View GitHub Profile
EXPLAIN ANALYZE SELECT * FROM words WHERE word = 'test';
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------
Index Scan using index_words_word on words (cost=0.42..8.44 rows=1 width=440) (actual time=1.628..1.631 rows=1 loops=1)
Index Cond: ((word)::text = 'test'::text)
Planning time: 0.269 ms
Execution time: 1.686 ms
EXPLAIN ANALYZE SELECT * FROM words WHERE word LIKE 't%' LIMIT 10;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..32.10 rows=10 width=440) (actual time=68.487..100.961 rows=10 loops=1)
-> Seq Scan on words (cost=0.00..40795.61 rows=12707 width=440) (actual time=68.485..100.959 rows=10 loops=1)
Filter: ((word)::text ~~ 't%'::text)
Rows Removed by Filter: 181269
Planning time: 7.296 ms
Execution time: 101.007 ms
@alekseyl
alekseyl / postgres_derailed_GIN_2.sql
Last active November 23, 2017 14:38
Fixed plan.
EXPLAIN ANALYZE SELECT id, title FROM "cards"
WHERE (title_tsv @@ to_tsquery( 'english', '(o:*|o)')) AND collection_id = 624::bigint
LIMIT 10;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=64.38..103.76 rows=10 width=57) (actual time=70.233..70.250 rows=10 loops=1)
-> Bitmap Heap Scan on cards (cost=64.38..210.07 rows=37 width=57) (actual time=70.231..70.248 rows=10 loops=1)
Recheck Cond: ((title_tsv @@ '''o'':* | ''o'''::tsquery) AND (collection_id = '624'::bigint))
Heap Blocks: exact=10
-> Bitmap Index Scan on index_examples_fts_with_collection (cost=0.00..64.37 rows=37 width=0) (actual time=70.204..70.204 rows=131 loops=1)
@alekseyl
alekseyl / derailed_index_example.sql
Last active November 23, 2017 12:14
derailed postgres index example on GIN without datatype convertion
EXPLAIN ANALYZE SELECT id, title FROM "cards"
WHERE (title_tsv @@ to_tsquery( 'english', '(o:*|o)')) AND collection_id = 624
LIMIT 10;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=408.55..447.93 rows=10 width=57) (actual time=273.000..273.103 rows=10 loops=1)
-> Bitmap Heap Scan on cards (cost=408.55..554.24 rows=37 width=57) (actual time=272.999..273.101 rows=10 loops=1)
Recheck Cond: ((collection_id = 624) AND (title_tsv @@ '''o'':* | ''o'''::tsquery) )
Heap Blocks: exact=10
-> BitmapAnd (cost=408.55..408.55 rows=37 width=0) (actual time=272.973..272.973 rows=0 loops=1)
class AutocompleteControllerDoc
include Swagger::Blocks
swagger_path '/autocomplete.json' do
operation :get,
summary: 'Fetches autocompletes on Cards titles and tags',
description: 'Returns nearest completions for words in card titles and tags,'\
' respects card restrictions and privacy rules',
tags: ['autocomple'] do
@alekseyl
alekseyl / mini-apivore-example.rb
Last active October 23, 2021 13:33
mini-apivore example
#cards_api_test.rb
require 'test_helper'
require 'mini_apivore_helper'
class CardsApiTest < MiniApivoreTest
#------- DEFINE CLASS SPECIFIC NAMED ROUTE HELPERS ----------------
def __get_cards(expectation)
check_route( :get, '/cards.json', expectation )
end
@alekseyl
alekseyl / swagger-docs-exmaple-quote.rb
Created November 8, 2017 15:22
swagger doc example for my note from sitepoint.com/do-the-right-thing-and-document-your-rails-api-with-swagger/
class Api::V1::UsersController < ApplicationController
.....
# POST /users
swagger_api :create do
summary "To create user"
notes "Implementation notes, such as required params, example queries for apis are written here."
param :form, "user[name]", :string, :required, "Name of user"
param :form, "user[age]", :integer, :optional, "Age of user"
param_list :form, "user[status]", :string, :required, "Status of user, can be active or inactive"
response :success
class UsersController < ApplicationController
resource_description do
formats [:json]
api_versions 'public'
end
api :POST, '/users' 'Create user'
description 'Create user with specifed user params'
param :user, Hash, desc: 'User information' do
@alekseyl
alekseyl / minitest-apidoc-quote.rb
Created November 8, 2017 13:52
minitest apidoc quote for API-comparision tech note
require_relative "../../spec_helper"
document Albums::Create do
meta :group, "Albums"
meta :request_method, "POST"
meta :request_path, "/albums"
meta :description, "Creates a new album with the given parameters."
param "name", "Name of the album", required: true
@alekseyl
alekseyl / rswag_quote.rb
Created November 8, 2017 13:46
rswag quote for API documenting note
# spec/integration/blogs_spec.rb
require 'swagger_helper'
describe 'Blogs API' do
path '/blogs' do
post 'Creates a blog' do
tags 'Blogs'
consumes 'application/json', 'application/xml'