logo
down
shadow

Strange use of the index in Mysql


Strange use of the index in Mysql

By : sscout
Date : November 22 2020, 07:01 PM
hope this fix your issue The MySQL optimizer makes a lot of decisions that sometimes look strange. In this particular case, I believe that you have a very small table (188 rows total from the looks of the first EXPLAIN), and that is affecting the optimizer's decision.
The "How MySQL Uses Indexes" manual pages offers this snippet of info:
code :


Share : facebook icon twitter icon
PHP MySQL create duplicate record on unique index strange fail

PHP MySQL create duplicate record on unique index strange fail


By : Allgroo
Date : March 29 2020, 07:55 AM
I hope this helps . Question: Why does a successful insert not run the respond function at the end of the code block?
I assume your respond method has an exit or die in it somewhere that halts execution.
code :
$response["message"] = "failed to insert because ".$stmt -> error;
// connect to server
$mysqli = new mysqli("localhost", "dbuser", 'dbpassword', "database");
if(mysqli_connect_errno()) {
    $response["message"] = "unable to connect to the MySQL server at 'localhost' as 'dbuser' using a password or select the database 'database' because ".mysqli_connect_errno();
    respond($response);
}

// build and run query
if ($stmt = $mysqli -> prepare("INSERT INTO `database`.`table` (`id`, `name`) VALUES (NULL, ?);")) {
    $stmt -> bind_param("s", $inputs["name"]);
    $success = $stmt -> execute();
    if ($success) {
        $response["status"] = "success";
        $response["id"] = $mysqli -> insert_id;
        respond($response);
    } else {
        $response["message"] = "failed to insert because ".$stmt -> error;
        respond($response); // <-- this is the bit you need
    }

    $stmt -> close();
} else {
    $response["message"] = "unable to build query because ".$mysqli -> error;
    respond($response);
}

$mysqli -> close();
Strange behavior: Undefined index whilst index IS correct

Strange behavior: Undefined index whilst index IS correct


By : user2524695
Date : March 29 2020, 07:55 AM
may help you . unset() always works. It has no return value and it won't raise an error or notice.
Unset example with undefined var: http://ideone.com/4OcSkQ
code :
<?php 
unset($a);
    $c[$i]['specName'] = $c[$i]['spec']; // <--- Undefined index notice
    if(array_key_exists('spec', $c[$i]) === true) { // test, if spec key exists
       $c[$i]['specName'] = $c[$i]['spec'];         // assign from spec to specName
    }
    else {
        echo 'Key "spec" was not found. Working on: ' . $i['name'] . '<br>';
    }
    unset(
       $c[$i]['battlegroup'], 
       $c[$i]['level'], 
       $c[$i]['achievementPoints'], 
       $c[$i]['thumbnail']
    );
Does wildcard in left-most column of composite index mean remaining columns in index aren't used in index lookup (MySQL)

Does wildcard in left-most column of composite index mean remaining columns in index aren't used in index lookup (MySQL)


By : Lenon Bordini
Date : March 29 2020, 07:55 AM
This might help you Here are your questions. Plural. By rephrasing them (with "in other words") they are just different questions. Doing so does not make it easier for responders necessarily. On the contrary.
Q1: [Title question] Does wildcard in left-most column of composite index mean remaining columns in index aren't used in index lookup (MySQL)?
code :
WHERE first_name LIKE 'joh%'
drop table myNames;
create table myNames
(   id int auto_increment primary key,
    lastname varchar(100) not null,
    firstname varchar(100) not null,
    col4 int not null,
    key(lastname,firstname)
);
truncate table myNames;
insert myNames (lastName,firstName,col4) values
('Smith','John',1),('Smithers','JohnSomeone',1),('Smith3','John4324',1),('Smi','Jonathan',1),('Smith123x$FA','Joh',1),('Smi3jfif','jkdid',1),('r3','fe2',1);

insert myNames (lastName,firstName,col4) select lastname,firstname,col4 from mynames;
insert myNames (lastName,firstName,col4) select lastname,firstname,col4 from mynames;
insert myNames (lastName,firstName,col4) select lastname,firstname,col4 from mynames;
insert myNames (lastName,firstName,col4) select lastname,firstname,col4 from mynames;
insert myNames (lastName,firstName,col4) select lastname,firstname,col4 from mynames;
insert myNames (lastName,firstName,col4) select lastname,firstname,col4 from mynames;
insert myNames (lastName,firstName,col4) select lastname,firstname,col4 from mynames;
insert myNames (lastName,firstName,col4) select lastname,firstname,col4 from mynames;
insert myNames (lastName,firstName,col4) select lastname,firstname,col4 from mynames;
insert myNames (lastName,firstName,col4) select lastname,firstname,col4 from mynames;
insert myNames (lastName,firstName,col4) select lastname,firstname,col4 from mynames;
insert myNames (lastName,firstName,col4) select lastname,firstname,col4 from mynames;
insert myNames (lastName,firstName,col4) select lastname,firstname,col4 from mynames;
insert myNames (lastName,firstName,col4) select lastname,firstname,col4 from mynames;
insert myNames (lastName,firstName,col4) select lastname,firstname,col4 from mynames;
insert myNames (lastName,firstName,col4) select lastname,firstname,col4 from mynames;

select count(*) from myNames; 
-- 458k rows

select count(*)
from myNames
where lastname like 'smi%';
-- 393216 rows

select count(*)
from myNames
where lastname like 'smi%' and firstname like 'joh%';
-- 262144 rows
explain 
select count(*) 
from myNames 
where lastname like 'smi%';
+----+-------------+---------+-------+---------------+----------+---------+------+--------+--------------------------+
| id | select_type | table   | type  | possible_keys | key      | key_len | ref  | rows   | Extra                    |
+----+-------------+---------+-------+---------------+----------+---------+------+--------+--------------------------+
|  1 | SIMPLE      | myNames | range | lastname      | lastname | 302     | NULL | 233627 | Using where; Using index |
+----+-------------+---------+-------+---------------+----------+---------+------+--------+--------------------------+

explain 
select count(*) 
from myNames 
where lastname like 'smi%' and firstname like 'joh%' and col4=1;
+----+-------------+---------+-------+---------------+----------+---------+------+--------+--------------------------+
| id | select_type | table   | type  | possible_keys | key      | key_len | ref  | rows   | Extra                    |
+----+-------------+---------+-------+---------------+----------+---------+------+--------+--------------------------+
|  1 | SIMPLE      | myNames | range | lastname      | lastname | 604     | NULL | 233627 | Using where; Using index |
+----+-------------+---------+-------+---------------+----------+---------+------+--------+--------------------------+


-- the below chunk is interest. Look at the Extra column

explain 
select count(*) 
from myNames 
where lastname like 'smi%' and firstname like 'joh%' and col4=1;
+----+-------------+---------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows   | Extra       |
+----+-------------+---------+------+---------------+------+---------+------+--------+-------------+
|  1 | SIMPLE      | myNames | ALL  | lastname      | NULL | NULL    | NULL | 457932 | Using where |
+----+-------------+---------+------+---------------+------+---------+------+--------+-------------+

explain 
select count(*) 
from myNames 
where firstname like 'joh%';
+----+-------------+---------+-------+---------------+----------+---------+------+--------+--------------------------+
| id | select_type | table   | type  | possible_keys | key      | key_len | ref  | rows   | Extra                    |
+----+-------------+---------+-------+---------------+----------+---------+------+--------+--------------------------+
|  1 | SIMPLE      | myNames | index | NULL          | lastname | 604     | NULL | 453601 | Using where; Using index |
+----+-------------+---------+-------+---------------+----------+---------+------+--------+--------------------------+


analyze table myNames;
+----------------------+---------+----------+----------+
| Table                | Op      | Msg_type | Msg_text |
+----------------------+---------+----------+----------+
| so_gibberish.mynames | analyze | status   | OK       |
+----------------------+---------+----------+----------+

select count(*) 
from myNames where left(lastname,3)='smi';
-- 393216 -- the REAL #
select count(*) 
from myNames where left(lastname,3)='smi' and left(firstname,3)='joh';
-- 262144 -- the REAL #

explain 
select lastname,firstname 
from myNames  
where lastname like 'smi%' and firstname like 'joh%';
+----+-------------+---------+-------+---------------+----------+---------+------+--------+--------------------------+
| id | select_type | table   | type  | possible_keys | key      | key_len | ref  | rows   | Extra                    |
+----+-------------+---------+-------+---------------+----------+---------+------+--------+--------------------------+
|  1 | SIMPLE      | myNames | range | lastname      | lastname | 604     | NULL | 226800 | Using where; Using index |
+----+-------------+---------+-------+---------------+----------+---------+------+--------+--------------------------+
Strange index behavior mysql

Strange index behavior mysql


By : Nashview
Date : March 29 2020, 07:55 AM
Hope this helps For ORDER BY ... LIMIT queries it will often be beneficial to use a query execution plan that avoids sorting. This is not necessarily because the sorting is expensive, but because it makes it possible to stop the query execution once the number of requested rows (here 50) are found.
In your case, if one starts with table a, the full join result will have to be generated before selecting the "top" 50 rows. If you start with scanning table o using the PRIMARY index, the join result will be sorted on o.id, and the join execution can stop once 50 rows have been found.
How to force mysql UPDATE query to use index? How to enable mysql engine to automatically use the index instead of forci

How to force mysql UPDATE query to use index? How to enable mysql engine to automatically use the index instead of forci


By : k.vijay
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Below is the update query/query plan that is not using the compound index that was created recently. The explain shows that its not using the compound index named radacctupdate which i think will make the update query faster. There are other indexes on table too which are used by other queries. , The update query can be forced to use index using below method:
Related Posts Related Posts :
  • Refactoring SQL
  • Create view or SP, only if the DB contains a pattern
  • Sql joining a table
  • Optimize the sql query
  • SQL server management studio Express
  • insert into select from in a table with identity column
  • Is is faster to filter and get data or filter then get data?
  • SQL server 2005, syntax error
  • SQL SELECT Join?
  • How to limit results by SUM
  • Need Help with SQL Subquery
  • SQL Join Tables
  • pipelined function
  • how to generate primary key values while inserting data into table through pl/sql stored procedure
  • Calling stored procedure from another stored procedure and returning result as new columns
  • Are these tables respect the 3NF Database Normalization?
  • Complex query making site extremely slow
  • SQL Full-Text Indexing Issue
  • finding a relationship accross many tables
  • Using Parameter Values In SQL Statement
  • oracle sql plus spool
  • Multiple rows update trigger
  • SQL - Conditionally evaluate WHERE clauses
  • Locking User account created under Windows Authentication in SQL Server
  • trigger execution against condition satisfaction
  • oracle sql developer is truncating my results
  • “Function” calling inside stored procedure
  • Excel VBA to Update SQL Table
  • I've caught an exception!! Now what?
  • How to select the last two records for each topic_id in MySQL
  • SQL Server 2005 triggered audit tables moved to SQL Server 2008, now trigger does not respond when trying to insert row
  • Using NHibernate to insert/update using a SQL server-side DEFAULT value
  • How to convert column type from varchar to date in PostgreSQL?
  • Combining the UNIQUE and CHECK constraints
  • MS SQL Server decimal data type rounds up
  • sp_addlinkedserver on sql server 2005 giving problem
  • Transitive SQL query on same table
  • Oracle - pl sql selecting from SYS_REFCURSOR
  • Broken count(*) after adding LEFT JOIN
  • Product Catalog Schema design
  • ODBC Connection String Problem
  • What's a reasonable number of rows and tables to be able to join in MySQL?
  • CFQuery where param
  • Split a Column in SQL Server
  • What does the % symbol do at the end of a string in SQL?
  • sql server 2005 express - Invalid Object Name error
  • Merge overlapping date intervals
  • Loop Control within a DataflowTask in ETL
  • SQL | How to always round up regardless of the last integer value, even when that may be 0
  • how to convert any char to - in sql server?
  • how to create function in postgres
  • Need to speed up the results of this SQL statement. Any advice?
  • How do I retrieve 10 days data up to date 'XYZ' from mysql with Perl when some days data are missing, i.e. public holida
  • How to export data with Oracle SQL Developer?
  • Aggregate SQL column values by time period
  • Are soft deletes a good idea?
  • DBCC SHRINKFILE 1 sproc for multiple databases
  • SQL VBA syntax issue
  • How to select a subset of results from a select statement
  • Oracle SQL: ROLLUP not summing correctly
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk