Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9258491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:25:07+00:00 2026-06-18T12:25:07+00:00

I have this table for a cardgame with 2-4 players: CREATE TABLE IF NOT

  • 0

I have this table for a cardgame with 2-4 players:

CREATE TABLE IF NOT EXISTS `cardgame` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `players` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `p1` mediumint(6) unsigned NOT NULL DEFAULT '0',
  `p2` mediumint(6) unsigned NOT NULL DEFAULT '0',
  `p3` mediumint(6) unsigned NOT NULL DEFAULT '0',
  `p4` mediumint(6) unsigned NOT NULL DEFAULT '0',
  `p1_state` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `p2_state` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `p3_state` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `p4_state` tinyint(1) unsigned NOT NULL DEFAULT '0',
  /* other rows */,
  `game_state` tinyint(1) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=79218 DEFAULT CHARSET=utf8;

I have to check all games, where I am a player. (my user_id is 1981 in the example)

mysql> EXPLAIN SELECT id FROM cardgame 
WHERE ((p1=1981 AND p1_state=1) OR (p2=1981 AND p2_state=1) 
OR (p3=1981 AND p3_state=1) OR (p4=1981 AND p4_state=1)) 
AND game_state < 7;
+----+-------------+----------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table    | type | possible_keys | key  | key_len | ref  | rows  | Extra       |
+----+-------------+----------+------+---------------+------+---------+------+-------+-------------+
|  1 | SIMPLE      | cardgame | ALL  | NULL          | NULL | NULL    | NULL | 79208 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+-------+-------------+

On which rows can I create indexes?
the p1, p1_state, p2.. p3.. p4, p4_state rows needs the index plus the game_state, but isn’t it too much?

Or I have to redesign the schema in some way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:25:08+00:00Added an answer on June 18, 2026 at 12:25 pm

    Or I have to redesign the schema in some way?

    Yes, normalise your schema:

    CREATE TABLE game_players (
      game_id  MEDIUMINT NOT NULL,
      position TINYINT   NOT NULL,
      player   MEDIUMINT NOT NULL,
      state    TINYINT UNSIGNED NOT NULL DEFAULT 0,
      PRIMARY KEY (game_id, position),
      UNIQUE (game_id, player), -- if a player can only be in each game once
      INDEX (player, state)
    )
    SELECT id, 1, p1, p1_state FROM cardgame
      UNION ALL
    SELECT id, 2, p2, p2_state FROM cardgame
      UNION ALL
    SELECT id, 3, p3, p3_state FROM cardgame
      UNION ALL
    SELECT id, 4, p4, p4_state FROM cardgame;
    
    ALTER TABLE cardgame 
      DROP p1, DROP p1_state,
      DROP p2, DROP p2_state,
      DROP p3, DROP p3_state,
      DROP p4, DROP p4_state;
    

    Then your query becomes:

    SELECT g.id
    FROM   game_players p JOIN cardgame g ON p.game_id = g.id
    WHERE  p.player = 1981 AND p.state = 1
       AND g.game_state < 7;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this table CREATE TABLE `codes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
I have this table create table utilizator( utilizatorId bigint not null auto_increment primary key,
I have this table CREATE TABLE [dbo].[friend_blocked_list]( [subdomain] [varchar](50) NOT NULL, [un] [nvarchar](50) NOT
I have this table: CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255)
I have this table: create table demo ( key number(10) not null, type varchar2(3)
I have this table: CREATE TABLE IF NOT EXISTS `events` ( `evt_id` int(11) NOT
I have this table: CREATE TABLE `maindb`.`daily_info` ( `di_date` date NOT NULL, `di_sid` int(10)
I have this table.. CREATE TABLE [dbo].[Tipo_Servicios_Info]( [TSI_TS_Id] [int] NOT NULL, [TS_Tar_Id] [int] NOT
I have this table right now CREATE TABLE [dbo].[DatosLegales]( [IdCliente] [int] NOT NULL, [IdDatoLegal]
I have this table structure on a SQL Server 2008 R2 database: CREATE TABLE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.