optimized the optimizations (fixed mariadb)
This commit is contained in:
parent
3131b0c839
commit
ba3b726c3d
|
@ -338,8 +338,7 @@ class MariaDBDatabase:
|
||||||
db=self.db_config['db'],
|
db=self.db_config['db'],
|
||||||
minsize=5,
|
minsize=5,
|
||||||
maxsize=20,
|
maxsize=20,
|
||||||
charset='utf8mb4',
|
charset='utf8mb4'
|
||||||
cursorclass=DictCursor
|
|
||||||
)
|
)
|
||||||
await self._create_tables()
|
await self._create_tables()
|
||||||
await self._create_indexes()
|
await self._create_indexes()
|
||||||
|
@ -351,7 +350,7 @@ class MariaDBDatabase:
|
||||||
async def _create_tables(self):
|
async def _create_tables(self):
|
||||||
"""Create necessary tables if they don't exist."""
|
"""Create necessary tables if they don't exist."""
|
||||||
async with self.pool.acquire() as conn:
|
async with self.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor(DictCursor) as cursor:
|
||||||
await cursor.execute("""
|
await cursor.execute("""
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
user_id BIGINT PRIMARY KEY,
|
user_id BIGINT PRIMARY KEY,
|
||||||
|
@ -402,7 +401,7 @@ class MariaDBDatabase:
|
||||||
async def _create_indexes(self):
|
async def _create_indexes(self):
|
||||||
"""Create performance indexes."""
|
"""Create performance indexes."""
|
||||||
async with self.pool.acquire() as conn:
|
async with self.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor(DictCursor) as cursor:
|
||||||
# Create indexes for better performance
|
# Create indexes for better performance
|
||||||
indexes = [
|
indexes = [
|
||||||
"CREATE INDEX IF NOT EXISTS idx_users_username ON users(username)",
|
"CREATE INDEX IF NOT EXISTS idx_users_username ON users(username)",
|
||||||
|
@ -435,7 +434,7 @@ class MariaDBDatabase:
|
||||||
async def get_user(self, user_id: int) -> Optional[UserData]:
|
async def get_user(self, user_id: int) -> Optional[UserData]:
|
||||||
"""Get user data by ID with associated servers."""
|
"""Get user data by ID with associated servers."""
|
||||||
async with self.pool.acquire() as conn:
|
async with self.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor(DictCursor) as cursor:
|
||||||
await cursor.execute("""
|
await cursor.execute("""
|
||||||
SELECT u.*, GROUP_CONCAT(us.server_id) AS servers
|
SELECT u.*, GROUP_CONCAT(us.server_id) AS servers
|
||||||
FROM users u
|
FROM users u
|
||||||
|
@ -470,7 +469,7 @@ class MariaDBDatabase:
|
||||||
async def save_user(self, user_data: UserData):
|
async def save_user(self, user_data: UserData):
|
||||||
"""Save or update user data with transaction."""
|
"""Save or update user data with transaction."""
|
||||||
async with self.pool.acquire() as conn:
|
async with self.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor(DictCursor) as cursor:
|
||||||
try:
|
try:
|
||||||
# Start transaction
|
# Start transaction
|
||||||
await cursor.execute("START TRANSACTION")
|
await cursor.execute("START TRANSACTION")
|
||||||
|
@ -527,7 +526,7 @@ class MariaDBDatabase:
|
||||||
async def save_server(self, server_id: int, server_name: str):
|
async def save_server(self, server_id: int, server_name: str):
|
||||||
"""Save server information."""
|
"""Save server information."""
|
||||||
async with self.pool.acquire() as conn:
|
async with self.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor(DictCursor) as cursor:
|
||||||
await cursor.execute("""
|
await cursor.execute("""
|
||||||
INSERT INTO servers (server_id, server_name)
|
INSERT INTO servers (server_id, server_name)
|
||||||
VALUES (%s, %s)
|
VALUES (%s, %s)
|
||||||
|
@ -542,7 +541,7 @@ class MariaDBDatabase:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
async with self.pool.acquire() as conn:
|
async with self.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor(DictCursor) as cursor:
|
||||||
placeholders = ','.join(['%s'] * len(server_ids))
|
placeholders = ','.join(['%s'] * len(server_ids))
|
||||||
await cursor.execute(f"""
|
await cursor.execute(f"""
|
||||||
SELECT server_id, server_name
|
SELECT server_id, server_name
|
||||||
|
@ -556,7 +555,7 @@ class MariaDBDatabase:
|
||||||
async def add_server_to_user(self, user_id: int, server_id: int):
|
async def add_server_to_user(self, user_id: int, server_id: int):
|
||||||
"""Add a server to user's server list."""
|
"""Add a server to user's server list."""
|
||||||
async with self.pool.acquire() as conn:
|
async with self.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor(DictCursor) as cursor:
|
||||||
await cursor.execute("""
|
await cursor.execute("""
|
||||||
INSERT IGNORE INTO user_servers (user_id, server_id)
|
INSERT IGNORE INTO user_servers (user_id, server_id)
|
||||||
VALUES (%s, %s)
|
VALUES (%s, %s)
|
||||||
|
@ -565,7 +564,7 @@ class MariaDBDatabase:
|
||||||
async def get_all_users(self) -> List[UserData]:
|
async def get_all_users(self) -> List[UserData]:
|
||||||
"""Get all users from the database."""
|
"""Get all users from the database."""
|
||||||
async with self.pool.acquire() as conn:
|
async with self.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor(DictCursor) as cursor:
|
||||||
await cursor.execute("""
|
await cursor.execute("""
|
||||||
SELECT u.*, GROUP_CONCAT(us.server_id) AS servers
|
SELECT u.*, GROUP_CONCAT(us.server_id) AS servers
|
||||||
FROM users u
|
FROM users u
|
||||||
|
@ -578,7 +577,7 @@ class MariaDBDatabase:
|
||||||
async def get_users_paginated(self, offset: int = 0, limit: int = 100) -> List[UserData]:
|
async def get_users_paginated(self, offset: int = 0, limit: int = 100) -> List[UserData]:
|
||||||
"""Get users with pagination."""
|
"""Get users with pagination."""
|
||||||
async with self.pool.acquire() as conn:
|
async with self.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor(DictCursor) as cursor:
|
||||||
await cursor.execute("""
|
await cursor.execute("""
|
||||||
SELECT u.*, GROUP_CONCAT(us.server_id) AS servers
|
SELECT u.*, GROUP_CONCAT(us.server_id) AS servers
|
||||||
FROM users u
|
FROM users u
|
||||||
|
@ -593,7 +592,7 @@ class MariaDBDatabase:
|
||||||
async def search_users(self, query: str, offset: int = 0, limit: int = 100) -> List[UserData]:
|
async def search_users(self, query: str, offset: int = 0, limit: int = 100) -> List[UserData]:
|
||||||
"""Search users with pagination."""
|
"""Search users with pagination."""
|
||||||
async with self.pool.acquire() as conn:
|
async with self.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor(DictCursor) as cursor:
|
||||||
# Try full-text search first
|
# Try full-text search first
|
||||||
try:
|
try:
|
||||||
await cursor.execute("""
|
await cursor.execute("""
|
||||||
|
@ -629,7 +628,7 @@ class MariaDBDatabase:
|
||||||
async def get_user_count_total(self) -> int:
|
async def get_user_count_total(self) -> int:
|
||||||
"""Get total number of users."""
|
"""Get total number of users."""
|
||||||
async with self.pool.acquire() as conn:
|
async with self.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor(DictCursor) as cursor:
|
||||||
await cursor.execute("SELECT COUNT(*) as count FROM users")
|
await cursor.execute("SELECT COUNT(*) as count FROM users")
|
||||||
result = await cursor.fetchone()
|
result = await cursor.fetchone()
|
||||||
return result['count'] if result else 0
|
return result['count'] if result else 0
|
||||||
|
@ -640,7 +639,7 @@ class MariaDBDatabase:
|
||||||
return
|
return
|
||||||
|
|
||||||
async with self.pool.acquire() as conn:
|
async with self.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor(DictCursor) as cursor:
|
||||||
try:
|
try:
|
||||||
await cursor.execute("START TRANSACTION")
|
await cursor.execute("START TRANSACTION")
|
||||||
|
|
||||||
|
@ -707,7 +706,7 @@ class MariaDBDatabase:
|
||||||
async def get_users_by_server(self, server_id: int) -> List[UserData]:
|
async def get_users_by_server(self, server_id: int) -> List[UserData]:
|
||||||
"""Get all users that are members of a specific server."""
|
"""Get all users that are members of a specific server."""
|
||||||
async with self.pool.acquire() as conn:
|
async with self.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor(DictCursor) as cursor:
|
||||||
await cursor.execute("""
|
await cursor.execute("""
|
||||||
SELECT u.*, GROUP_CONCAT(us.server_id) AS servers
|
SELECT u.*, GROUP_CONCAT(us.server_id) AS servers
|
||||||
FROM users u
|
FROM users u
|
||||||
|
@ -721,7 +720,7 @@ class MariaDBDatabase:
|
||||||
async def get_user_count(self) -> int:
|
async def get_user_count(self) -> int:
|
||||||
"""Get total number of users in database."""
|
"""Get total number of users in database."""
|
||||||
async with self.pool.acquire() as conn:
|
async with self.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor(DictCursor) as cursor:
|
||||||
await cursor.execute("SELECT COUNT(*) as user_count FROM users")
|
await cursor.execute("SELECT COUNT(*) as user_count FROM users")
|
||||||
result = await cursor.fetchone()
|
result = await cursor.fetchone()
|
||||||
return result['user_count'] if result else 0
|
return result['user_count'] if result else 0
|
||||||
|
@ -729,7 +728,7 @@ class MariaDBDatabase:
|
||||||
async def get_server_count(self) -> int:
|
async def get_server_count(self) -> int:
|
||||||
"""Get total number of unique servers."""
|
"""Get total number of unique servers."""
|
||||||
async with self.pool.acquire() as conn:
|
async with self.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor(DictCursor) as cursor:
|
||||||
await cursor.execute("SELECT COUNT(DISTINCT server_id) as server_count FROM user_servers")
|
await cursor.execute("SELECT COUNT(DISTINCT server_id) as server_count FROM user_servers")
|
||||||
result = await cursor.fetchone()
|
result = await cursor.fetchone()
|
||||||
return result['server_count'] if result else 0
|
return result['server_count'] if result else 0
|
||||||
|
@ -742,7 +741,7 @@ class MariaDBDatabase:
|
||||||
}
|
}
|
||||||
|
|
||||||
async with self.pool.acquire() as conn:
|
async with self.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor(DictCursor) as cursor:
|
||||||
await cursor.execute("""
|
await cursor.execute("""
|
||||||
SELECT server_id, COUNT(user_id) as user_count
|
SELECT server_id, COUNT(user_id) as user_count
|
||||||
FROM user_servers
|
FROM user_servers
|
||||||
|
|
Loading…
Reference in a new issue