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

  • SEARCH
  • Home
  • 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 490311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:53:46+00:00 2026-05-13T01:53:46+00:00

I have a table structure like this (vertical design). I can have unlimited number

  • 0

I have a table structure like this (vertical design). I can have unlimited number of attributes (eg: city, phone etc.) for each user.

Table: tbl_UserAttributes

┌────────┬───────────┬────────────┐
| UserID │ FieldName │ Value      |
├────────┼───────────┼────────────┤
│ 341    │ city      │ MyCity1    │
│ 772    │ phone     │ 1234567890 │
│ 033    │ city      │ MyCity2    │
│ 044    │ sex       │ M          │
│ 772    │ firstname │ MyName     │
│ ---    │ ---       │ ---        │
└────────┴───────────┴────────────┘

I have to implement a search feature which should output rows which we apply query like this for a horizontally designed table:

SELECT 
    FieldName 
FROM 
    tbl_UserAttributes 
WHERE 
    city='%Mumbai%' AND 
    sex='M' AND ...

Please dont ask me to change the database design.

UPDATE: At present, I have a JOIN solution in place which is very slow and it hangs the server some times. Any alternate methods?

  • 1 1 Answer
  • 2 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-05-13T01:53:47+00:00Added an answer on May 13, 2026 at 1:53 am

    EAV table is a good thing as long as you don’t need to search for multiple values at once in which case it becomes a bad thing.

    You cannot index several values at once because they are located in different records.

    In an SQL Server table you could create an indexed view over multiple values and use it for the searches.

    In Oracle, you could cluster the table by UserID which would keep all records with the same UserID within one data page which would use an index on the most selective value and quickly scan for the other values.

    In PostgreSQL, you could store all value in a single array and index it with a GIN index.

    In MySQL, you can do neither of this.

    Here’s a query which will return the values:

    SELECT  *
    FROM    tbl_UserAttributes tcity
    JOIN    tbl_UserAttributes tsex
    ON      tsex.userid = tcity.userid
    WHERE   tcity.fieldname = 'city'
            AND tcity.value LIKE '%Mumbai%'
            AND tsex.fieldname = 'sex'
            AND tsex.value = 'M'
    

    but don’t expect it to be very fast.

    Update:

    If you needed to have the exact matching, you could create a composite index on (fieldname, value, userid), put the most selective fieldname into the first table and use STRAIGHT_JOIN to force the order:

    SELECT  *
    FROM    tbl_UserAttributes tcity
    STRAIGHT_JOIN
            tbl_UserAttributes tsex
    ON      tsex.userid = tcity.userid
    WHERE   tcity.fieldname = 'city'
            AND tcity.value = 'Mumbai'
            AND tsex.fieldname = 'sex'
            AND tsex.value = 'M'
    

    However, this won’t help with your current query, since you are looking for a wildcard match in which case the indexes are not very helpful. And your second table won’t benefit much from the index unless you are querying a maternity hospital database.

    Still it will save you some time since the index scan can be used instead of a table scan.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table structure that looks like this: Columns: Account Number Service Code
I have a table structure like this: <table> <tr> <td> <ul> <li class=check></li> </ul>
I have my Table structure like this :: ATT_Table : Fields - Act_ID, Assigned_To_ID,
If I have a table structure like this: Transaction [TransID, ...] Document [DocID, TransID,
I have a table structure with columns like this [ID] [Name] [ParentId] [ParentName] The
I currently have a table structure that looks something like this(some details omitted): ColumnName
I have an activity table with a structure like this: id prd_id act_dt grp
I have this table structure and would like to map it using Fluent Hibernate
I have a table Action with a (part of the) structure like this: Action
Hi i have a table structure like this. table **geoinfo** id name desc I

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.