![Page 1: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/1.jpg)
Javascript & SQL within database management system
![Page 2: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/2.jpg)
![Page 3: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/3.jpg)
Clusterpoint — Building and Growing Database-as-a-Service : A Case Study
Clusterpoint database2-in-1: DBaaS & on-premises database
Document oriented
Sharded + replicated
Schema less
ACID transactions
Cloud enabled (EU & US sites)
Clusterpoint — Building and Growing Database-as-a-Service : A Case Study
![Page 4: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/4.jpg)
Clusterpoint — Building and Growing Database-as-a-Service : A Case Study
Evolution of Computing InfrastructureHistory
Clusterpoint — Building and Growing Database-as-a-Service : A Case Study
1970 1995 2010 20152005
Google discovers that all of web does not fit in a relational database
Jeff Dean, Sanjay Ghemaway et al publish papers on MapReduce
and BigTable
Key-Value Store
MapReduce
Mainframes Clusters of Commodity Hardware Commercially Viable
Technologies are split between: data storage
and computing will they merge
?
![Page 5: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/5.jpg)
Clusterpoint — Building and Growing Database-as-a-Service : A Case StudyClusterpoint — Building and Growing Database-as-a-Service : A Case Study
![Page 6: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/6.jpg)
Clusterpoint — Running JavaScript Inside the Database
db.runCommand({mapreduce: "DenormAggCollection",query: { filter1: { '$in': [ 'A', 'B' ] }, filter2: 'C', filter3: { '$gt': 123 } },map: function() { emit( { d1: this.Dim1, d2: this.Dim2 }, { msum: this.measure1, recs: 1, mmin: this.measure1, mmax: this.measure2 < 100 ? this.measure2 : 0 } );},reduce: function(key, vals) { var ret = { msum: 0, recs: 0, mmin: 0, mmax: 0 }; for(var i = 0; i < vals.length; i++) { ret.msum += vals[i].msum; ret.recs += vals[i].recs; if(vals[i].mmin < ret.mmin) ret.mmin = vals[i].mmin; if((vals[i].mmax < 100) && (vals[i].mmax > ret.mmax)) ret.mmax = vals[i].mmax; } return ret; },finalize: function(key, val) { val.mavg = val.msum / val.recs; return val; },out: 'result1',verbose: true});db.result1. find({ mmin: { '$gt': 0 } }). sort({ recs: -1 }). skip(4). limit(8);
SELECT Dim1, Dim2, SUM(Measure1) AS MSum, COUNT(*) AS RecordCount, AVG(Measure2) AS MAvg, MIN(Measure1) AS MMin MAX(CASE WHEN Measure2 < 100 THEN Measure2 END) AS MMaxFROM DenormAggTableWHERE (Filter1 IN (’A’,’B’)) AND (Filter2 = ‘C’) AND (Filter3 > 123)GROUP BY Dim1, Dim2HAVING (MMin > 0)ORDER BY RecordCount DESCLIMIT 4, 8
1
2
3
4
5
1
7
6
1
2
3
4
5
Grouped dimension columns are pulled
out as keys in the map function,
reducing the size of the working set.
Measures must be manually aggregated.
Aggregates depending on record counts
must wait until finalization.
Measures can use procedural logic.
Filters have an ORM/ActiveRecord-
looking style.
6 Aggregate filtering must be applied to
the result set, not in the map/reduce.
7 Ascending: 1; Descending: -1
Revis
ion 4
, C
reate
d 2
010-0
3-0
6
Ric
k O
sborne, ric
kosborne.o
rg
mySQL MongoDB
![Page 7: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/7.jpg)
Clusterpoint — Building and Growing Database-as-a-Service : A Case Study
+
![Page 8: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/8.jpg)
Clusterpoint — Building and Growing Database-as-a-Service : A Case Study
Technology top 2015 (StackOverflow)
Clusterpoint — Building and Growing Database-as-a-Service : A Case Study
![Page 9: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/9.jpg)
Clusterpoint — Running JavaScript Inside the Database
SQL JavaScriptflexible to express
queries
executes in parallel
static
hard to define expressions
bad with custom routines
hard to express queries
difficult to execute in parallel
dynamic
easy to define expressions
great with custom routines
![Page 10: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/10.jpg)
Clusterpoint — Running JavaScript Inside the Database
Javascript - V8Too good to be used only in browsers
Clusterpoint — Running JavaScript Inside the Database
• Chrome • Node.js • MongoDB • Google BigQuery UDF
![Page 11: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/11.jpg)
Clusterpoint — Running JavaScript Inside the Database
Javascript - V8Produces machine code (IA-32, x64, ARM)
Clusterpoint — Running JavaScript Inside the Database
![Page 12: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/12.jpg)
Clusterpoint — Running JavaScript Inside the Database
Javascript - V8Performance - Problem
Clusterpoint — Running JavaScript Inside the Database
Compute the 25,000th prime
![Page 13: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/13.jpg)
Clusterpoint — Running JavaScript Inside the Database
Javascript - V8Performance - Algorithm
Clusterpoint — Running JavaScript Inside the Database
For x = 1 to infinity: if x not divisible by any member of an initially empty list of primes, add x to the list until we have 25,000
![Page 14: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/14.jpg)
Clusterpoint — Running JavaScript Inside the Database
Javascript - V8Performance - Contenders
Clusterpoint — Running JavaScript Inside the Database
![Page 15: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/15.jpg)
Clusterpoint — Running JavaScript Inside the Database
Javascript - V8Performance - Results (only 17% slower)
Clusterpoint — Running JavaScript Inside the Database
![Page 16: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/16.jpg)
Javascript - V8Data Binding
Clusterpoint — Running JavaScript Inside the Database
C++ callback V8 if (name == “John”) { return “Male”;
}
Accessor(C++ callback)
string * get_value(string &field_name) { if (field_name == “name”)
return new string(“John”); else
return new string(“Unknown”); }
![Page 17: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/17.jpg)
Clusterpoint — Building and Growing Database-as-a-Service : A Case Study
JS/SQLLanguage structure
SQL-like structure
Arbitrary JavaScript in any clause of the SELECT or UPDATE statement.
Joins, indexing nested documents +
Clusterpoint — Building and Growing Database-as-a-Service : A Case Study
![Page 18: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/18.jpg)
SELECT * FROM product
+Clusterpoint — Building and Growing Database-as-a-Service : A Case Study
![Page 19: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/19.jpg)
JS/SQLInsert statement
INSERT INTO product JSON VALUE { "name": "Schwinn S29 Full Suspension Mountain Bike", "image_url": "schwinn_s29.jpeg", "description": "...", "color": ["black","red"], "order_price": 211.16, "price": 259.16, "packaging": { "height": 23, "width": 25, "depth": 12, "weight": 54 }, "availability": "In Stock" } +
Clusterpoint — Building and Growing Database-as-a-Service : A Case Study
![Page 20: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/20.jpg)
JS/SQLUpdate statement
Clusterpoint — Running JavaScript Inside the Database
+
UPDATE product[“id_123”] SET { color.push_back(“orange”); packaging.volume = packaging.height * packaging.width * packaging.depth; }
![Page 21: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/21.jpg)
JS/SQLPrice buckets
+Clusterpoint — Building and Growing Database-as-a-Service : A Case Study
![Page 22: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/22.jpg)
JS/SQLGrouping/Aggregation
function PriceBucket(price) { var boundaries = [0, 1, 5, 10, 50, 100, 200, 500, 1000]; for (var i = 1; i < boundaries.length; i++) { if (price >= boundaries[i - 1] && price < boundaries[i]) return boundaries[i - 1].toString() + " to " + boundaries[i].toString(); } return "above " + boundaries[boundaries.length - 1].toString(); }
SELECT PriceBucket(price), COUNT() FROM product GROUP BY PriceBucket(price); +Clusterpoint — Building and Growing Database-as-a-Service : A Case Study
![Page 23: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/23.jpg)
JS/SQLJavascript & SQL in action
SELECT PriceBucket(price), COUNT() FROM product GROUP BY PriceBucket(price);
+Clusterpoint — Building and Growing Database-as-a-Service : A Case Study
doc1 John
AccessorField (price) store JS V8
…. PriceBucket(price) …. doc2 George
doc3 Marry
![Page 24: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/24.jpg)
JS/SQLJoins
Clusterpoint — Running JavaScript Inside the Database
INSERT INTO product["34A40855"] JSON VALUE { name: "Schwinn S29 Full Suspension Mountain Bike", price: 259.16 };
INSERT INTO order JSON VALUE { product_key: "34A40855", delivery_address: "My Office" };
SELECT delivery_address, product[product_key].price FROM order WHERE product[product_key].price > 20 +
![Page 25: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/25.jpg)
Sign up
Where:
http://cloud.clusterpoint.com
How Much?
“Zero, none”
10GB free FOREVER !!!
Any Docs?
http://docs.clusterpoint.com
Few fields and you are in
Clusterpoint — Running JavaScript Inside the Database
![Page 26: Javascript & SQL within database management system](https://reader034.vdocuments.site/reader034/viewer/2022042907/5883eb331a28ab34428b5025/html5/thumbnails/26.jpg)
Clusterpoint — Building and Growing Database-as-a-Service : A Case Study
Thank you!