Skip to content
This repository has been archived by the owner on Dec 3, 2019. It is now read-only.

Fixed performance regression caused by treating all numbers as numerics #231

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ object PostgreSQLColumnEncoderRegistry {
class PostgreSQLColumnEncoderRegistry extends ColumnEncoderRegistry {

private val classesSequence_ : List[(Class[_], (ColumnEncoder, Int))] = List(
classOf[Int] -> (IntegerEncoderDecoder -> ColumnTypes.Numeric),
classOf[java.lang.Integer] -> (IntegerEncoderDecoder -> ColumnTypes.Numeric),
classOf[Int] -> (IntegerEncoderDecoder -> ColumnTypes.Integer),
classOf[java.lang.Integer] -> (IntegerEncoderDecoder -> ColumnTypes.Integer),

classOf[java.lang.Short] -> (ShortEncoderDecoder -> ColumnTypes.Numeric),
classOf[Short] -> (ShortEncoderDecoder -> ColumnTypes.Numeric),
classOf[java.lang.Short] -> (ShortEncoderDecoder -> ColumnTypes.Smallint),
classOf[Short] -> (ShortEncoderDecoder -> ColumnTypes.Smallint),

classOf[Long] -> (LongEncoderDecoder -> ColumnTypes.Numeric),
classOf[java.lang.Long] -> (LongEncoderDecoder -> ColumnTypes.Numeric),
classOf[Long] -> (LongEncoderDecoder -> ColumnTypes.Bigserial),
classOf[java.lang.Long] -> (LongEncoderDecoder -> ColumnTypes.Bigserial),

classOf[String] -> (StringEncoderDecoder -> ColumnTypes.Varchar),
classOf[java.lang.String] -> (StringEncoderDecoder -> ColumnTypes.Varchar),

classOf[Float] -> (FloatEncoderDecoder -> ColumnTypes.Numeric),
classOf[java.lang.Float] -> (FloatEncoderDecoder -> ColumnTypes.Numeric),
classOf[Float] -> (FloatEncoderDecoder -> ColumnTypes.Real),
classOf[java.lang.Float] -> (FloatEncoderDecoder -> ColumnTypes.Real),

classOf[Double] -> (DoubleEncoderDecoder -> ColumnTypes.Numeric),
classOf[java.lang.Double] -> (DoubleEncoderDecoder -> ColumnTypes.Numeric),
classOf[Double] -> (DoubleEncoderDecoder -> ColumnTypes.Double),
classOf[java.lang.Double] -> (DoubleEncoderDecoder -> ColumnTypes.Double),

classOf[BigDecimal] -> (BigDecimalEncoderDecoder -> ColumnTypes.Numeric),
classOf[java.math.BigDecimal] -> (BigDecimalEncoderDecoder -> ColumnTypes.Numeric),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class NumericSpec extends Specification with DatabaseTestHelper {

val id = executePreparedStatement(handler, "INSERT INTO numeric_test DEFAULT VALUES RETURNING id").rows.get(0)("id")
executePreparedStatement(handler, "UPDATE numeric_test SET numcol = ? WHERE id = ?", Array[Any](1234, id))
executePreparedStatement(handler, "UPDATE numeric_test SET numcol = ? WHERE id = ?", Array[Any](123.123, id))

id === 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ import org.joda.time.LocalDate
import com.github.mauricio.async.db.util.Log
import com.github.mauricio.async.db.exceptions.InsufficientParametersException
import java.util.UUID

import com.github.mauricio.async.db.postgresql.exceptions.GenericDatabaseException

import scala.concurrent.Await
import scala.concurrent.duration.{Duration, SECONDS}
import scala.util.Random

class PreparedStatementSpec extends Specification with DatabaseTestHelper {

val log = Log.get[PreparedStatementSpec]
Expand Down Expand Up @@ -371,6 +376,30 @@ class PreparedStatementSpec extends Specification with DatabaseTestHelper {
}
}

"not take twice the time as a non prepared statement" in {
withHandler {
handler =>
executeDdl(handler, "create temp table performance_test (id integer PRIMARY KEY, int1 integer)")
(1 to 2000).foreach(i =>
executeQuery(handler, s"insert into performance_test (id, int1) values ($i, ${Random.nextInt(20000)})"))

val preparedStatementStartTime = System.nanoTime()
(1 to 2000).foreach { i =>
val id = Random.nextInt(2000)
Await.result(handler.sendPreparedStatement("update performance_test set int1 = int1 where id = ?", Array(id)), Duration(5, SECONDS))
}
val preparedStatementTime = System.nanoTime() - preparedStatementStartTime

val plainQueryStartTime = System.nanoTime()
(1 to 2000).foreach { i =>
val id = Random.nextInt(2000)
Await.result(handler.sendQuery(s"update performance_test set int1 = int1 where id = $id"), Duration(5, SECONDS))
}
val plainQueryTime = System.nanoTime() - plainQueryStartTime

preparedStatementTime must beLessThan(plainQueryTime * 2)
}
}
}

}