How to optimize MySQL database or a script?

Good day, have a database mysql with a lot of records in two tables in total 100 000 000 records, there is also a Web server on the basis of the ligament Apache + NGINX, which spits out the result of sql queries. Interested in the opportunity to reduce the request processing time. Write something of this kind:

Date Eid Gid Number Ecode Status
2017-10-23 11:44:00 ULC_TEST_C220 ULC_TEST_C220 +77272314212 16 Y


An excerpt from the php script:

$codes=array("1","3","16","17","19","20","21","27","31","41","86","111");
 foreach($codes as $code)
{
 switch ($code)
{
 case 1:
 $query_input = "SELECT COUNT(`eid`) from input where eid='$name' and (gid like '$marker1' or gid like '$marker2')and number='$number' AND ecode='$code' AND Date between '$datefrom' and '$dateto'";
$SQLin=$dbConnect->query($query_input);
 $query_out = "SELECT COUNT(`eid`) from output where eid='$name' and (gid like '$marker1' or gid like '$marker2') and number='$number' AND ecode='$code' AND Date between '$datefrom' and '$dateto' ";
$SQLout=$dbConnect->query($query_out);

 while ($datain=mysqli_fetch_array($SQLin))
{
$countin1=$datain[0];
}
 while ($dataout=mysqli_fetch_array($SQLout))
{
$countout1=$dataout[0];
}
 break;
June 10th 19 at 15:21
2 answers
June 10th 19 at 15:23
I like don't like))
$marker1, $marker2 - that is, give an example?
field name number - exactly the database does not swear?

well, in the first approximation, I would have half of the request given to the CPU, not memory)))
June 10th 19 at 15:25
EXPLAIN and indexes to help you.

Find more questions by tags MySQL