mirror of
https://github.com/correl/mage.git
synced 2024-12-01 11:09:56 +00:00
SQL query perfomance improvements
There will be always 0 or 1 distinct result. If we don't limit this query it will find an item, and still will go through the whole table trying another one, which is unneccesary and have performance impact.
This commit is contained in:
parent
1d989a70db
commit
3be7598ee1
3 changed files with 20 additions and 23 deletions
|
@ -7,17 +7,18 @@ import com.j256.ormlite.stmt.QueryBuilder;
|
||||||
import com.j256.ormlite.support.ConnectionSource;
|
import com.j256.ormlite.support.ConnectionSource;
|
||||||
import com.j256.ormlite.support.DatabaseConnection;
|
import com.j256.ormlite.support.DatabaseConnection;
|
||||||
import com.j256.ormlite.table.TableUtils;
|
import com.j256.ormlite.table.TableUtils;
|
||||||
import java.io.File;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import mage.cards.repository.RepositoryUtil;
|
import mage.cards.repository.RepositoryUtil;
|
||||||
import mage.game.result.ResultProtos;
|
import mage.game.result.ResultProtos;
|
||||||
import mage.server.rating.GlickoRating;
|
import mage.server.rating.GlickoRating;
|
||||||
import mage.server.rating.GlickoRatingSystem;
|
import mage.server.rating.GlickoRatingSystem;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public enum UserStatsRepository {
|
public enum UserStatsRepository {
|
||||||
|
|
||||||
instance;
|
instance;
|
||||||
|
@ -68,7 +69,7 @@ public enum UserStatsRepository {
|
||||||
public UserStats getUser(String userName) {
|
public UserStats getUser(String userName) {
|
||||||
try {
|
try {
|
||||||
QueryBuilder<UserStats, Object> qb = dao.queryBuilder();
|
QueryBuilder<UserStats, Object> qb = dao.queryBuilder();
|
||||||
qb.where().eq("userName", userName);
|
qb.limit(1L).where().eq("userName", userName);
|
||||||
List<UserStats> users = dao.query(qb.prepare());
|
List<UserStats> users = dao.query(qb.prepare());
|
||||||
if (users.size() == 1) {
|
if (users.size() == 1) {
|
||||||
return users.get(0);
|
return users.get(0);
|
||||||
|
@ -92,7 +93,7 @@ public enum UserStatsRepository {
|
||||||
public long getLatestEndTimeMs() {
|
public long getLatestEndTimeMs() {
|
||||||
try {
|
try {
|
||||||
QueryBuilder<UserStats, Object> qb = dao.queryBuilder();
|
QueryBuilder<UserStats, Object> qb = dao.queryBuilder();
|
||||||
qb.orderBy("endTimeMs", false).limit(1);
|
qb.orderBy("endTimeMs", false).limit(1L);
|
||||||
List<UserStats> users = dao.query(qb.prepare());
|
List<UserStats> users = dao.query(qb.prepare());
|
||||||
if (users.size() == 1) {
|
if (users.size() == 1) {
|
||||||
return users.get(0).getEndTimeMs();
|
return users.get(0).getEndTimeMs();
|
||||||
|
@ -106,7 +107,7 @@ public enum UserStatsRepository {
|
||||||
// updateUserStats reads tables finished after the last DB update and reflects it to the DB.
|
// updateUserStats reads tables finished after the last DB update and reflects it to the DB.
|
||||||
// It returns the list of user names that are upated.
|
// It returns the list of user names that are upated.
|
||||||
public List<String> updateUserStats() {
|
public List<String> updateUserStats() {
|
||||||
HashSet<String> updatedUsers = new HashSet();
|
HashSet<String> updatedUsers = new HashSet<>();
|
||||||
// Lock the DB so that no other updateUserStats runs at the same time.
|
// Lock the DB so that no other updateUserStats runs at the same time.
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
long latestEndTimeMs = this.getLatestEndTimeMs();
|
long latestEndTimeMs = this.getLatestEndTimeMs();
|
||||||
|
|
|
@ -37,20 +37,17 @@ import com.j256.ormlite.stmt.Where;
|
||||||
import com.j256.ormlite.support.ConnectionSource;
|
import com.j256.ormlite.support.ConnectionSource;
|
||||||
import com.j256.ormlite.support.DatabaseConnection;
|
import com.j256.ormlite.support.DatabaseConnection;
|
||||||
import com.j256.ormlite.table.TableUtils;
|
import com.j256.ormlite.table.TableUtils;
|
||||||
import java.io.File;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.TreeSet;
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
import mage.cards.CardSetInfo;
|
import mage.cards.CardSetInfo;
|
||||||
import mage.constants.CardType;
|
import mage.constants.CardType;
|
||||||
import mage.constants.SetType;
|
import mage.constants.SetType;
|
||||||
import mage.util.RandomUtil;
|
import mage.util.RandomUtil;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author North
|
* @author North
|
||||||
|
@ -65,12 +62,10 @@ public enum CardRepository {
|
||||||
private static final long CARD_DB_VERSION = 48;
|
private static final long CARD_DB_VERSION = 48;
|
||||||
// raise this if new cards were added to the server
|
// raise this if new cards were added to the server
|
||||||
private static final long CARD_CONTENT_VERSION = 65;
|
private static final long CARD_CONTENT_VERSION = 65;
|
||||||
|
private final TreeSet<String> landTypes = new TreeSet();
|
||||||
private Dao<CardInfo, Object> cardDao;
|
private Dao<CardInfo, Object> cardDao;
|
||||||
private Set<String> classNames;
|
private Set<String> classNames;
|
||||||
|
|
||||||
private final TreeSet<String> landTypes = new TreeSet();
|
|
||||||
|
|
||||||
CardRepository() {
|
CardRepository() {
|
||||||
File file = new File("db");
|
File file = new File("db");
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
|
@ -315,7 +310,7 @@ public enum CardRepository {
|
||||||
public CardInfo findCard(String setCode, String cardNumber) {
|
public CardInfo findCard(String setCode, String cardNumber) {
|
||||||
try {
|
try {
|
||||||
QueryBuilder<CardInfo, Object> queryBuilder = cardDao.queryBuilder();
|
QueryBuilder<CardInfo, Object> queryBuilder = cardDao.queryBuilder();
|
||||||
queryBuilder.where().eq("setCode", new SelectArg(setCode)).and().eq("cardNumber", cardNumber).and().eq("nightCard", false);
|
queryBuilder.limit(1L).where().eq("setCode", new SelectArg(setCode)).and().eq("cardNumber", cardNumber).and().eq("nightCard", false);
|
||||||
List<CardInfo> result = cardDao.query(queryBuilder.prepare());
|
List<CardInfo> result = cardDao.query(queryBuilder.prepare());
|
||||||
if (!result.isEmpty()) {
|
if (!result.isEmpty()) {
|
||||||
return result.get(0);
|
return result.get(0);
|
||||||
|
|
|
@ -7,12 +7,13 @@ import com.j256.ormlite.stmt.QueryBuilder;
|
||||||
import com.j256.ormlite.stmt.SelectArg;
|
import com.j256.ormlite.stmt.SelectArg;
|
||||||
import com.j256.ormlite.support.ConnectionSource;
|
import com.j256.ormlite.support.ConnectionSource;
|
||||||
import com.j256.ormlite.table.TableUtils;
|
import com.j256.ormlite.table.TableUtils;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.apache.log4j.Logger;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -112,7 +113,7 @@ public enum ExpansionRepository {
|
||||||
ExpansionInfo set = null;
|
ExpansionInfo set = null;
|
||||||
try {
|
try {
|
||||||
QueryBuilder<ExpansionInfo, Object> qb = expansionDao.queryBuilder();
|
QueryBuilder<ExpansionInfo, Object> qb = expansionDao.queryBuilder();
|
||||||
qb.where().eq("code", new SelectArg(setCode));
|
qb.limit(1L).where().eq("code", new SelectArg(setCode));
|
||||||
List<ExpansionInfo> expansions = expansionDao.query(qb.prepare());
|
List<ExpansionInfo> expansions = expansionDao.query(qb.prepare());
|
||||||
if (expansions.size() > 0) {
|
if (expansions.size() > 0) {
|
||||||
set = expansions.get(0);
|
set = expansions.get(0);
|
||||||
|
@ -126,7 +127,7 @@ public enum ExpansionRepository {
|
||||||
ExpansionInfo set = null;
|
ExpansionInfo set = null;
|
||||||
try {
|
try {
|
||||||
QueryBuilder<ExpansionInfo, Object> qb = expansionDao.queryBuilder();
|
QueryBuilder<ExpansionInfo, Object> qb = expansionDao.queryBuilder();
|
||||||
qb.where().eq("name", new SelectArg(setName));
|
qb.limit(1L).where().eq("name", new SelectArg(setName));
|
||||||
List<ExpansionInfo> expansions = expansionDao.query(qb.prepare());
|
List<ExpansionInfo> expansions = expansionDao.query(qb.prepare());
|
||||||
if (expansions.size() > 0) {
|
if (expansions.size() > 0) {
|
||||||
set = expansions.get(0);
|
set = expansions.get(0);
|
||||||
|
|
Loading…
Reference in a new issue