// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;

public class Denormalization {
    private static final int NSUB_PER_MAIN = 10;
    private static final int NMAIN = 100000;
    private static final int NFIND = 1000;
    private static Random rng = new Random();
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        testNormalized();
        testDenormalized();
    }
    private static void testNormalized() throws SQLException {
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Test", "", "");
        Statement stmt = con.createStatement();
        stmt.executeUpdate("CREATE TABLE main (mainid INTEGER, txt VARCHAR(30), PRIMARY KEY(mainid))");
        stmt.executeUpdate("CREATE TABLE sub (subid INTEGER, mainid INTEGER, PRIMARY KEY(subid))");
        stmt.executeUpdate("CREATE INDEX sub_mainid_ix ON sub(mainid)");
        for(int i = 0; i < NMAIN; i++) {
            stmt.executeUpdate("INSERT INTO main VALUES(" + i + ",'This is rec #" + i + "')");
            for(int j = 0; j < NSUB_PER_MAIN; j++) {
                stmt.executeUpdate("INSERT INTO sub VALUES(" + (i*10+j) + "," + i + ")");
            }
        }
        long t1 = System.currentTimeMillis();
        for(int k = 0; k < NFIND; k++) {
            int subid = rng.nextInt(NMAIN*NSUB_PER_MAIN);
            ResultSet rs = stmt.executeQuery("SELECT txt FROM main JOIN sub ON main.mainid=sub.mainid WHERE sub.subid=" + subid);
            if(!rs.next() && rs.next()) {
                throw new RuntimeException("Ooops");
            }
            rs.close();
        }
        long t2 = System.currentTimeMillis();
        System.out.println("Normalized: " + (t2-t1));
        stmt.executeUpdate("DROP INDEX sub_mainid_ix ON sub");
        stmt.executeUpdate("DROP TABLE sub");
        stmt.executeUpdate("DROP TABLE main");
        stmt.close();
        con.close();
    }
    private static void testDenormalized() throws SQLException {
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Test", "", "");
        Statement stmt = con.createStatement();
        stmt.executeUpdate("CREATE TABLE data (mainid INTEGER, txt VARCHAR(30), subids VARCHAR(100), PRIMARY KEY(mainid))");
        stmt.executeUpdate("CREATE INDEX data_subids_ix ON data(subids)");
        for(int i = 0; i < NMAIN; i++) {
            String subids = "";
            for(int j = 0; j < NSUB_PER_MAIN; j++) {
                if(j > 0) subids = subids + ",";
                subids = subids + (i*10+j);
            }
            stmt.executeUpdate("INSERT INTO data VALUES(" + i + ",'This is rec #" + i + "','" + subids + "')");
        }
        long t1 = System.currentTimeMillis();
        for(int k = 0; k < NFIND; k++) {
            int subid = rng.nextInt(NMAIN*NSUB_PER_MAIN);
            ResultSet rs = stmt.executeQuery("SELECT txt FROM data WHERE CONCAT(',',subids,',') LIKE '%," + subid + ",%'");
            if(!rs.next() && rs.next()) {
                throw new RuntimeException("Ooops");
            }
            rs.close();
        }
        long t2 = System.currentTimeMillis();
        System.out.println("Denormalized: " + (t2-t1));
        stmt.executeUpdate("DROP INDEX data_subids_ix ON data");
        stmt.executeUpdate("DROP TABLE data");
        stmt.close();
        con.close();
    }
}
