1 5 package org.h2.test.cases; 6 7 import java.sql.Connection ; 8 import java.sql.DriverManager ; 9 import java.sql.PreparedStatement ; 10 import java.sql.Statement ; 11 import java.util.Random ; 12 13 import org.h2.tools.DeleteDbFiles; 14 import org.h2.util.MemoryUtils; 15 16 public class TestLinearIndex { 17 public static void main(String [] args) throws Exception { 18 Class.forName("org.h2.Driver"); 19 int len = 1000; 21 for(int a=0; ; a++) { 22 testLoop(true, len); 23 testLoop(false, len); 24 len += 1000; 25 } 26 } 29 30 private static void testLoop(boolean hashIndex, int len) throws Exception { 31 DeleteDbFiles.execute(".", "test", true); 32 String url = "jdbc:h2:test"; 33 Connection conn = DriverManager.getConnection(url, "sa", "sa"); 34 Statement stat = conn.createStatement(); 35 stat.execute("drop table if exists test"); 36 stat.execute("create table test(id int, name varchar)"); 37 if(hashIndex) { 38 stat.execute("create unique hash index idxid on test(id)"); 39 } else { 40 stat.execute("create unique index idxid on test(id)"); 41 } 42 stat.execute("insert into test select x, 'Hello World' from system_range(1, "+len+")"); 43 PreparedStatement prep = conn.prepareStatement("select * from test where id=?"); 44 Random random = new Random (1); 45 long time = System.currentTimeMillis(); 46 for(int i=0; i<len*50; i++) { 47 prep.setInt(1, random.nextInt(len)); 48 prep.executeQuery(); 49 } 50 time = System.currentTimeMillis() - time; 51 System.out.println("len: " + len + " hash: " + hashIndex + " time: " + time+" used: " + MemoryUtils.getMemoryUsed()); 52 conn.close(); 53 } 54 55 } 56 | Popular Tags |