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 8910943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:49:21+00:00 2026-06-15T03:49:21+00:00

I’m trying to optimize a view in MySql. The view takes 0-5 records from

  • 0

I’m trying to optimize a view in MySql. The view takes 0-5 records from a table and turns them into 1 record. The view works, but is slowing down as the # of records in the property_log table increases.

For example, data that looks like this:

mysql> select * from property_log where event_id = 1144882;
+----------+--------------+------------------------------+
| event_id | log_key      | log_value                    |
+----------+--------------+------------------------------+
|  1144882 | userId       | 1000                         |
|  1144882 | licenseId    | 3                            |
|  1144882 | messageTypeId| 7                            |
|  1144882 | message      | Sample message               |
|  1144882 | op           | tracking                     |
+----------+--------------+------------------------------+

Gets turned into 1 record:

mysql> select * from view_logged_property where id = 1144882
+---------+--------+-----------+---------------+-------------------+
| ID      | UserID | LicenseID | MessageTypeID | Message           |
+---------+--------+-----------+---------------+-------------------+
| 1144882 | 1000   | 3         | 7             | Sample message    |
+---------+--------+-----------+---------------+-------------------+

Edit: Important Note – Not all 5 records will be present all the time. For example, the data could also look like this:

mysql> select * from property_log where event_id = 1144882;
+----------+--------------+------------------------------+
| event_id | log_key      | log_value                    |
+----------+--------------+------------------------------+
|  1144882 | userId       | 1000                         |
|  1144882 | messageTypeId| 7                            |
|  1144882 | message      | Sample message               |
|  1144882 | op           | tracking                     |
+----------+--------------+------------------------------+

The view I’m currently using to do this is the following:

DROP VIEW IF EXISTS view_logged_property;
CREATE VIEW view_logged_property as (
  SELECT
    p.event_id as ID,
    (select log_value from property_log where log_key = "userId" and event_id = p.event_id) as UserID,
    (select log_value from property_log where log_key = "licenseId" and event_id = p.event_id) as LicenseID,
    (select log_value from property_log where log_key = "messageTypeId" and event_id = p.event_id) as MessageTypeID,
    (select log_value from property_log where log_key = "message" and event_id = p.event_id) as Message
  FROM
    logging_event p
  WHERE
    p.event_id in (select event_id from property_log where log_key = "op" and log_value = "tracking")
);

What’s the best way to write this view so it’ll perform well even as the # of records in the “property_log” table increases?

  • 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-15T03:49:23+00:00Added an answer on June 15, 2026 at 3:49 am

    I would use joins in this case.

    Changed after comment left joins should solve problem.

    DROP VIEW IF EXISTS view_logged_property;
    CREATE VIEW view_logged_property as (
        select 
            p1.event_id as ID, 
            p5.log_value as UserID, 
            p2.log_value as LicenseID, 
            p3.log_value as MessageTypeID, 
            p4.log_value as Message
        from property_log p1  
        left join property_log p5 on p1.event_id = p5.event_id and p5.log_key = 'userId'
        left join property_log p2 on p1.event_id = p2.event_id and p2.log_key = 'licenseId'
        left join property_log p3 on p1.event_id = p3.event_id and p3.log_key = 'messageTypeId'
        left join property_log p4 on p1.event_id = p4.event_id and p4.log_key = 'message'
        where p1.log_key = 'op' and p1.log_value = 'tracking'
    );
    

    Could you test it on your data?

    Index on pair event_id, log_key might be required, sth like below:

     create index event_key_idx on property_log(event_id, log_key);
    

    SQL Fiddle example

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a view passing on information from a database: def serve_article(request, id): served_article
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.