How can I optimize my SQL query in KTORKOTLIN server? - Stack Overflow

In my Ktor project, I am fetching train stats from the Train Table. The query I am using responds in 20

In my Ktor project, I am fetching train stats from the Train Table. The query I am using responds in 200ms, whereas all of my other queries take only 90ms.

Is there a way to optimize this query?

override suspend fun getMarkerStats(): TrainStats? {
    return try {
        dbFactory.dbQuery {

            val data = Trains.selectAll()
            val totalTrains = data.count { it[MapTrains.numb] != "" }
            val lateTrains = data.count { it[MapTrains.late] }
            val onTimeTrains =
                data.count { !it[MapTrains.late] && !it[MapTrains.cancelled] && !it[MapTrains.diverted] }
            val cancelledTrains = data.count { it[MapTrains.cancelled] }
            val divertedTrains = data.count { it[MapTrains.diverted] }

            TrainStats(
                running = totalTrains,
                late = lateTrains,
                onTime = onTimeTrains,
                cancelled = cancelledTrains,
                diverted = divertedTrains,
                trainCount = totalTrains
            )
        }
    } catch (e: Exception) {
        println("Error retrieving marker stats: ${e.message}")
       null
    }
}

I could not find way to get all data I need in one SQL query.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743910976a4528358.html

相关推荐

  • How can I optimize my SQL query in KTORKOTLIN server? - Stack Overflow

    In my Ktor project, I am fetching train stats from the Train Table. The query I am using responds in 20

    3天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信