How to optimize and find minimum from the two sql query, by writing it as single query? I have two queries. How can I write them into a single query for efficient performance? I need the minimum value of the result of $sql1 and $sql2. Is it possible?

$sql1="SELECT sum(pv_value) as pv FROM `{$wpdb-&gt;prefix}wpmlm_pv_meta` as M WHERE pv_meta like 'left_%' and user_id={$user_id}";
$sql2="SELECT sum(pv_value) as pv FROM `{$wpdb-&gt;prefix}wpmlm_pv_meta` as M WHERE pv_meta like 'right_%' and user_id={$user_id}";
From MySQL 8.0, you can use WITH to first filter for user_id:

WITH Raw AS (
  SELECT pv_value, pv_meta
  FROM `{$wpdb-&gt;prefix}wpmlm_pv_meta`
  WHERE user_id={$user_id}
), Raw2 AS (
  SELECT
    SUM(
      CASE WHEN pv_meta LIKE 'left_%' THEN pv_value ELSE 0 END
    ) AS leftSum,
    SUM(
      CASE WHEN pv_meta LIKE 'right_%' THEN pv_value ELSE 0 END
    ) AS rightSum
  FROM Raw
)
SELECT
  CASE
    WHEN leftSum &lt; rightSum THEN rightSum
    ELSE leftSum
  END
FROM Raw2


assuming that the external parameters are the same in the queries.

Your original query (queries) used two WHERE-conditions: one for filtering for the user_id, and one for a LIKE prefix search. The best working of the MySQL would be if it would first filter first for the user_id by an index, and then it would use a linear scan for the LIKE. However, there is no guarantee for it to do. It is possible, and even likely, that your query triggers a linear scan on the whole table.

This optimization not only reduces the required two searches to a single one, but it also enforces to commit the user_id scan first.

If you don't have an index of wpmlm_pv_meta(user_id) then create it.