MySQL ve MongoDB Benchmark

By in Blog on 03/12/2014

Kullanılan IDE: Oracle Jdeveloper 11.1.1.6

Kullanılan JDK: 1.6.0_30

Kullanılan RDBMS : MySQL 5.6 vs Kullanılan NoSQL: MongoDB 2.4.8

Kullanılan DB bağlantı arayüzü: JDBC

DB ler lokalde kuruludur. Network trafiği göz ardı edilebilir.

Test senaryosu: Aynı PC ile aynı field sayısı kadar bir tablo oluşturarak istenilen aynı sayıda kaydı yaratmak sonra da aynı koşul ile çekmek.

Özet sonuç: NoSQL veritabanı genel olarak çok daha hızlı görülüyor. Yalnız fetch işleminde RDBMS in daha hızlı olduğu görülüyor.

Ancak NoSQL veritabanları için göze çarpan dezavantajın ilişkisel tablolardan (collection) bir arada veri çekilmesi işlemindeki zorluk ve yavaşlık olduğu söylenmektedirBu nedenle bir de rdbms sistemde join ile çekilen query ler üzerinden kıyasa gitmek daha kesin yorum yapabilmek için anlamlı olacaktır.

Benchmark çalışmasında 1Milyon kayıt içerisinden 50bin adet döndüren bir sorgu yapılıp fetch edilmiştir.

MySQL:

totalTime of insert operation: 983 (saniye)

First Select time: 913 ms

First Fetch time of select operation in ms: 32

Second Select time: 845 ms

Second Fetch time of select operation in ms: 33

MongoDB:

totalTime of insert operation : 222 (saniye)

First Select time: 2 ms

First Fetch time of select operation in ms: 1323

Second Select time: 1 ms

Second Fetch time of select operation in ms: 1503

Rows of select count: 50000 (for both test)

Jdev projesi istenirse buradan indirilebilir. (password: ybs123)

Benchmark projesinin source kodları:

package com.benchmark;

import com.benchmark.util.DateUtility;

import com.mysql.jdbc.Connection;

import com.mysql.jdbc.Statement;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Random;

import java.util.UUID;

public class MySQL

{

private Connection con;

public static void main(String[] args)

{

new MySQL();

}

public MySQL()

{

try

{

connect();

createTable();

insertBenchmarkVeri();

selectTest();

}

catch (Exception e)

{

e.printStackTrace();

}

}

private void connect()

{

try

{

Class.forName(“com.mysql.jdbc.Driver”).newInstance();

con = (Connection)DriverManager.getConnection(“jdbc:mysql://localhost:3306/” + “test”, “root”, “admin”);

}

catch (Exception e)

{

e.printStackTrace();

}

}

private void createTable() throws SQLException

{

try

{

con.createStatement().execute(“DROP TABLE test”);

con.createStatement().execute(“CREATE TABLE test ” + “(id INTEGER not NULL AUTO_INCREMENT, ” + ” test1 VARCHAR(255), ” +

” test2 VARCHAR(255), ” + ” test3 VARCHAR(255), ” + ” test4 INTEGER, ” + ” PRIMARY KEY ( id ))”);

}

catch (SQLException e)

{

e.printStackTrace();

con.close();

}

}

private void insertBenchmarkVeri()

{

final Statement st;

try

{

st = (Statement)con.createStatement();

Random generator = new Random();

String c, d, e;

int x = 0;

int ayniKayitLimit = 50000; // select yapildiginda donecek kayit sayisi

int nrCount = 0;

long startTime, totalTime;

startTime = System.currentTimeMillis();

System.out.println(“startTime for insert operation in ms: “+startTime);

for (int i = 0; i < 1000000; i++)

{

if (i % 250000 == 0)

{

System.gc();

}

final String randomString = UUID.randomUUID().toString();

c = randomString.substring(0, 5);

d = randomString.substring(5, 10);

e = randomString.substring(10, 15);

if (ayniKayitLimit >= nrCount)

{

nrCount++;

st.execute(“INSERT INTO `test` (`test1`, `test2`, `test3`, `test4`) VALUES (‘” + c + “‘, ‘” + d + “‘, ‘” + e + “‘, 12345)”);

} else

{

x = generator.nextInt(9999);

st.execute(“INSERT INTO `test` (`test1`, `test2`, `test3`, `test4`) VALUES (‘” + c + “‘, ‘” + d + “‘, ‘” + e + “‘, ‘” + x + “‘)”);

}

}

totalTime = DateUtility.getSecondsBetween(startTime, System.currentTimeMillis());

System.out.println(“totalTime of insert operation in second: “+totalTime);

}

catch (SQLException e)

{

e.printStackTrace();

}

}

private void selectTest()

{

ResultSet res;

long startTime, totalTime;

Statement st;

try

{

st = (Statement)con.createStatement();

startTime = System.currentTimeMillis();

res = st.executeQuery(“SELECT * FROM test WHERE `test4`=12345”);

totalTime = System.currentTimeMillis() – startTime;

System.out.println(“Select time: ” + totalTime + ” ms”);

startTime = System.currentTimeMillis();

int count = 0;

while (res.next())

{

count++;

}

totalTime = DateUtility.getSecondsBetween(startTime, System.currentTimeMillis());

System.out.println(“Fetch time of select operation in second: “+totalTime);

System.out.println(“Rows of select count: ” + count);

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

package com.benchmark;

import com.benchmark.util.DateUtility;

import com.mongodb.BasicDBObject;

import com.mongodb.DB;

import com.mongodb.DBCollection;

import com.mongodb.DBCursor;

import com.mongodb.MongoClient;

import java.sql.SQLException;

import java.util.Random;

import java.util.UUID;

public class Mongo

{

private DB db;

private DBCollection collection;

MongoClient mongoClient;

public static void main(String[] args)

{

new Mongo();

}

public Mongo()

{

connect();

selectTest();

}

private void connect()

{

try

{

// To connect to mongodb server

mongoClient = new MongoClient(“localhost”, 27017);

// Now connect to your databases

db = mongoClient.getDB(“test”);

System.out.println(“Connected to database successfully”); //Connected to db, if db is not exist, would be created automatically.

// boolean auth = db.authenticate(“mustafa”, “benchmark”.toCharArray());

// System.out.println(“Authentication: ” + auth);

}

catch (Exception e)

{

System.err.println(e.getClass().getName() + “: ” + e.getMessage());

}

}

private void createTable()

{

try

{

db.getCollection(“test”).drop();

collection = db.getCollection(“test”);

System.out.println(“Collection created successfully”);

}

catch (Exception e)

{

e.printStackTrace();

}

}

private void insertBenchmarkVeri()

{

int x = 0;

BasicDBObject document;

Random generator = new Random();

String c, d, e;

int ayniKayitLimit = 50000; // select yapildiginda donecek kayit sayisi

int nrCount = 0;

long startTime, totalTime;

try

{

startTime = System.currentTimeMillis();

System.out.println(“startTime for insert operation in ms: ” + startTime + ” sys date: ” + DateUtility.getToday());

for (int i = 0; i < 1000000; i++)

{

if (i % 10000 == 0)

{

System.gc();

}

final String randomString = UUID.randomUUID().toString();

c = randomString.substring(0, 5);

d = randomString.substring(5, 10);

e = randomString.substring(10, 15);

if (ayniKayitLimit >= nrCount)

{

nrCount++;

document = new BasicDBObject();

document.put(“test1”, “” + c + “”);

document.put(“test2”, “” + d + “”);

document.put(“test3”, “” + e + “”);

document.put(“test4”, 12345);

collection.insert(document);

} else

{

x = generator.nextInt(9999);

document = new BasicDBObject();

document.put(“test1”, “” + c + “”);

document.put(“test2”, “” + d + “”);

document.put(“test3”, “” + e + “”);

document.put(“test4”, “” + x + “”);

collection.insert(document);

}

}

totalTime = DateUtility.getSecondsBetween(startTime, System.currentTimeMillis());

System.out.println(“totalTime of insert operation in second: ” + totalTime);

}

catch (Exception exc)

{

exc.printStackTrace();

}

}

private void selectTest()

{

long startTime, totalTime;

try

{

collection = db.getCollection(“test”);

BasicDBObject query;

DBCursor cursor;

query = new BasicDBObject();

query.put(“test4”, 12345);

startTime = System.currentTimeMillis();

cursor = collection.find(query);

totalTime = System.currentTimeMillis() – startTime;

System.out.println(“Select time: ” + totalTime + ” ms”);

startTime = System.currentTimeMillis();

int count = 0;

while (cursor.hasNext())

{

count++;

cursor.next();

}

totalTime = System.currentTimeMillis() – startTime;

System.out.println(“Fetch time of select operation in ms: ” + totalTime);

System.out.println(“Rows of select count: ” + count);

}

catch (Exception exc)

{

exc.printStackTrace();

}

}

}

(Visited 72 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *