Slick对numThreads和最佳性能感到困惑 [英] Slick confused about numThreads and best practice for good performance

查看:80
本文介绍了Slick对numThreads和最佳性能感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Slick的PlayFrameWork,并在所有I/O数据库繁重的系统中使用它.在我的application.conf文件中,我有以下设置:

I am using the PlayFrameWork with Slick and using it in a system that is all I/O database heavy. In my application.conf file I have this setting:

play {
  akka {
    akka.loggers = ["akka.event.slf4j.Slf4jLogger"]
    loglevel = WARNING
    actor {
      default-dispatcher = {
        fork-join-executor {
          parallelism-factor = 20.0
        }
      }
    }
  }
}

显然,这为我提供了一个Play应用程序每个内核20个线程,据我了解,Slick创建了它自己的线程池,是Slick中的NumThreads字段,它表示线程总数或(NumThreads x CPU)?是否有最佳实践来获得最佳性能?我目前将我的设置配置为:

This obviously gives me 20 threads per core for the play application and as I understand it Slick creates it's own threadpool, is the NumThreads field in Slick mean that that's the total number of threads or is it (NumThreads x CPU's)? And is there any best practice for best performance? I currently have my settings configured as:

database {
  dataSourceClass = "org.postgresql.ds.PGSimpleDataSource"
  properties = {
    databaseName = "dbname"
    user = "postgres"
    password = "password"
  }
  numThreads = 10
}

推荐答案

numThreads 是线程池中的简单线程数. Slick使用此线程池执行查询.

numThreads is simple number of thread in Thread pool. Slick use this thread pool for executing querying.

The following config keys are supported for all connection pools, both built-in and third-party:

  • numThreads(整数,可选,默认值:20):线程池中用于异步执行的并发线程数 数据库操作.有关更多信息,请参见HikariCP Wiki. 正确调整线程池的大小.请注意,对于异步 在Slick中执行时,您应该调整线程池大小(这是 参数),而不是最大连接池大小.

  • numThreads (Int, optional, default: 20): The number of concurrent threads in the thread pool for asynchronous execution of database actions. See the HikariCP wiki for more imformation about sizing the thread pool correctly. Note that for asynchronous execution in Slick you should tune the thread pool size (this parameter) accordingly instead of the maximum connection pool size.

queueSize(整数,可选,默认值:1000):数据库操作的队列大小,当所有 线程很忙.超出此限制,新操作将立即失败. 设置为0表示无队列(直接切换),设置为-1表示无限制 队列大小(不建议).

queueSize (Int, optional, default: 1000): The size of the queue for database actions which cannot be executed immediately when all threads are busy. Beyond this limit new actions fail immediately. Set to 0 for no queue (direct hand-off) or to -1 for an unlimited queue size (not recommended).

默认情况下,已将池调整为异步执行.除了连接参数,在大多数情况下,您只需要设置numThreads和queueSize即可.在这种情况下,线程池(通过其队列)存在争用,而不是连接存在争用,因此您可以对最大连接数有相当大的限制(基于数据库服务器仍然可以处理的数量,而不是最大的数量).高效的).当对事务中的非数据库操作进行排序时,Slick将使用的连接数将超过池中线程的数量.

The pool is tuned for asynchronous execution by default. Apart from the connection parameters you should only have to set numThreads and queueSize in most cases. In this scenario there is contention over the thread pool (via its queue), not over the connections, so you can have a rather large limit on the maximum number of connections (based on what the database server can still handle, not what is most efficient). Slick will use more connections than there are threads in the pool when sequencing non-database actions inside a transaction.

HikariCP支持以下配置键:

  • url(字符串,必填):JDBC URL

  • url (String, required): JDBC URL

driver或driverClassName(字符串,可选):要加载用户的JDBC驱动程序类(字符串,可选)*:用户名

driver or driverClassName (String, optional): JDBC driver class to load user (String, optional)*: User name

密码(字符串,可选):密码

隔离(字符串,可选):新连接的事务隔离级别.允许的值为:NONE,READ_COMMITTED, READ_UNCOMMITTED,REPEATABLE_READ,SERIALIZABLE.

isolation (String, optional): Transaction isolation level for new connections. Allowed values are: NONE, READ_COMMITTED, READ_UNCOMMITTED, REPEATABLE_READ, SERIALIZABLE.

目录(字符串,可选):新连接的默认目录.

catalog (String, optional): Default catalog for new connections.

readOnly(布尔型,可选):新连接的只读标志.

readOnly (Boolean, optional): Read Only flag for new connections.

属性(地图,可选):传递给驱动程序或数据源的属性.

properties (Map, optional): Properties to pass to the driver or DataSource.

dataSourceClass(字符串,可选):JDBC驱动程序提供的DataSource类的名称.这优于使用 司机.请注意,设置此键后,网址将被忽略(您必须 可以使用属性来配置数据库连接.)

dataSourceClass (String, optional): The name of the DataSource class provided by the JDBC driver. This is preferred over using driver. Note that url is ignored when this key is set (You have to use properties to configure the database connection instead).

maxConnections(整数,可选,默认值:numThreads * 5):池中的最大连接数.

minConnections(整数,可选,默认值:与numThreads相同):要保留在池中的最小连接数.

connectionTimeout(持续时间,可选,默认值:1s):对getConnection的调用超时之前要等待的最长时间.如果这 没有连接可用就超过了时间, 将抛出SQLException.最小值为1000毫秒.

connectionTimeout (Duration, optional, default: 1s): The maximum time to wait before a call to getConnection is timed out. If this time is exceeded without a connection becoming available, a SQLException will be thrown. 1000ms is the minimum value.

validationTimeout(持续时间,可选,默认值:1s):将测试连接的活动性的最长时间. 1000毫秒 是最小值.

validationTimeout (Duration, optional, default: 1s): The maximum amount of time that a connection will be tested for aliveness. 1000ms is the minimum value.

idleTimeout(持续时间,可选,默认值:10分钟):允许连接在池中空闲的最长时间. 值0表示从不删除空闲连接. 水池.

idleTimeout (Duration, optional, default: 10min): The maximum amount of time that a connection is allowed to sit idle in the pool. A value of 0 means that idle connections are never removed from the pool.

maxLifetime(持续时间,可选,默认值:30min):最大值 池中连接的生存期.当空闲连接到达时 此超时,即使最近使用过,也会从 水池.值为0表示没有最大寿命.

maxLifetime (Duration, optional, default: 30min): The maximum lifetime of a connection in the pool. When an idle connection reaches this timeout, even if recently used, it will be retired from the pool. A value of 0 indicates no maximum lifetime.

connectionInitSql(字符串,可选):一条SQL语句,它将在每次创建新连接后执行,然后再将其添加到 水池.如果此SQL无效或引发异常,它将为 被视为连接失败,并且标准重试逻辑将被 跟着.

connectionInitSql (String, optional): A SQL statement that will be executed after every new connection creation before adding it to the pool. If this SQL is not valid or throws an exception, it will be treated as a connection failure and the standard retry logic will be followed.

initializationFailFast(布尔值,可选,默认值:false): 控制如果无法恢复池,池是否将快速失败" 初始连接成功播种.如果无法连接 如果在池启动时创建,则会抛出RuntimeException. 如果minConnections为0,则此属性无效.

initializationFailFast (Boolean, optional, default: false): Controls whether the pool will "fail fast" if the pool cannot be seeded with initial connections successfully. If connections cannot be created at pool startup time, a RuntimeException will be thrown. This property has no effect if minConnections is 0.

leakDetectionThreshold(持续时间,可选,默认值:0):在出现消息之前,连接可以离开池的时间 已记录,表明可能存在连接泄漏.值0表示泄漏 检测被禁用.启用泄漏的最低可接受值 检测时间为10秒.

leakDetectionThreshold (Duration, optional, default: 0): The amount of time that a connection can be out of the pool before a message is logged indicating a possible connection leak. A value of 0 means leak detection is disabled. Lowest acceptable value for enabling leak detection is 10s.

connectionTestQuery(字符串,可选):一条语句 将在从连接获得连接之前执行该操作 池以验证与数据库的连接仍然有效. 它是数据库相关的,应该是一个花费很少的查询 由数据库处理(例如"VALUES 1").未设置时,JDBC4 改为使用Connection.isValid()方法(通常是 最好).

connectionTestQuery (String, optional): A statement that will be executed just before a connection is obtained from the pool to validate that the connection to the database is still alive. It is database dependent and should be a query that takes very little processing by the database (e.g. "VALUES 1"). When not set, the JDBC4 Connection.isValid() method is used instead (which is usually preferable).

registerMbeans(布尔值,可选,默认值:false):是否已注册JMX管理Bean("MBeans").

registerMbeans (Boolean, optional, default: false): Whether or not JMX Management Beans ("MBeans") are registered.

Slick具有非常透明的配置设置.最佳性能的最佳实践,没有经验法则.这取决于您的数据库(提供多少个并行连接)和您的应用程序.这完全是关于在数据库和数据库之间进行调优.应用程序.

Slick have very transparent configuration setting.Best practice for good performance, There is no thumb rule. It depends on your database(how many parallel connection provides) and your application. It is all about tuning between database & application.

这篇关于Slick对numThreads和最佳性能感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆