Skip to content

Instantly share code, notes, and snippets.

View kgorman's full-sized avatar

Kenny Gorman kgorman

View GitHub Profile
@matheusoliveira
matheusoliveira / json_manipulator.sql
Last active February 17, 2024 15:14
Simple PostgreSQL functions to manipulate json objects. (Note: performance is not a concern for those functions)
CREATE OR REPLACE FUNCTION public.json_append(data json, insert_data json)
RETURNS json
IMMUTABLE
LANGUAGE sql
AS $$
SELECT ('{'||string_agg(to_json(key)||':'||value, ',')||'}')::json
FROM (
SELECT * FROM json_each(data)
UNION ALL
SELECT * FROM json_each(insert_data)
@kgorman
kgorman / gist:977336
Created May 17, 2011 20:38
Data Density Example Script
// data density example
// shows how storing/sorting data by key can reduce I/O drastically in MongoDB
// diskloc gives file # and offset of a given document, divide by 512 for block #
// 2011 kcg
// start clean
db.disktest_noorg.drop();
db.disktest_org.drop();
// create some random data in non userid dense form
@kgorman
kgorman / gist:977320
Created May 17, 2011 20:32
Data Compaction/Locality Example
> db.disktest_noorg.ensureIndex({"userid":-1});
> db.disktest_org.ensureIndex({"userid":-1});
> db.disktest_noorg.find().sort({userid:-1})
{ "_id" : ObjectId("4dd2d82b6a2e502b3043ef33"), "userid" : 49999, "imageid" : 20, "img" : "www.kennygorman.com/foo.jpg", "title" : "This is a sample title", "data" : "79357fb65ba7b87f2632dfe8e098400c" }
{ "_id" : ObjectId("4dd2d82e6a2e502b30442186"), "userid" : 49999, "imageid" : 14, "img" : "www.kennygorman.com/foo.jpg", "title" : "This is a sample title", "data" : "43a98d693fc469b323f2c9033176cd97" }
{ "_id" : ObjectId("4dd2d8316a2e502b30448652"), "userid" : 49999, "imageid" : 18, "img" : "www.kennygorman.com/foo.jpg", "title" : "This is a sample title", "data" : "810c9ca8b61eff972e0d514191874f22" }
{ "_id" : ObjectId("4dd2d82a6a2e502b3043d4d4"), "userid" : 49996, "imageid" : 39, "img" : "www.kennygorman.com/foo.jpg", "title" : "This is a sample title", "data" : "e864723614597f09c6d09469aa3e3235" }
{ "_id" : ObjectId("4dd2d82b6a2e502b3043efcd"), "userid" : 49995, "ima
@kgorman
kgorman / mongodbcheats.js
Last active March 1, 2024 19:16
MongoDB cheat sheet
/* MongoDB cheat sheet */
// replication lag via serverStatus()
db._adminCommand( { serverStatus : 1 , repl ; 2 } )
// stats
db.stats()
db.foo.stats()
// size of BSON of some query