简体   繁体   English

从MySQL检索数据到React Native时获取速度慢

[英]Slow fetch when retrieving data from MySQL to React Native

I am not sure what could be causing the fetch to be so slow. 我不确定是什么原因导致读取速度太慢。 Maybe it could be React Native's fetch or maybe it's the way my query is set up? 可能是React Native的获取,还是我的查询设置方式?

Here is my fetch for React Native: 这是我对React Native的获取:

    forceUpdateHandler(){
  fetch(`https://www.example.com/React/user-profile.php?username=${this.state.username}` , {
  method: 'GET',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  }

 })
   .then((response) => response.json())
   .then((responseJson) => {
     this.setState({
       isLoading: false,
       dataSource: responseJson,
       user_image:  responseJson[0].user_images.map(item => ({ ...item, progress: new Animated.Value(0), unprogress: new Animated.Value(1) })),
       },function() {


       });
   })
   .catch((error) => {
     //console.error(error);
   });
}

And here is my PHP Query for the mysql: 这是我对mysql的PHP查询:

 if ($conn->connect_error) {

 die("Connection failed: " . $conn->connect_error);
} 
 // Getting the received JSON into $json variable.
 $json = file_get_contents('php://input');

 // decoding the received JSON and store into $obj variable.
 $obj = json_decode($json,true);

// Populate Username from JSON $obj array and store into $username.
$username = $_GET['username'];

$result = mysqli_query($conn, "SELECT * FROM users WHERE username='$username'");
$fetch = mysqli_query($conn, "SELECT id, images, note, date FROM user_images WHERE username='$username' ORDER BY date DESC"); 

// I think, you'll get a single row, so no need to loop
$json = mysqli_fetch_array($result, MYSQL_ASSOC);

$json2 = array();
while ($row = mysqli_fetch_assoc($fetch)){
    //Our YYYY-MM-DD date.
    $ymd = $row['date'];

    //Convert it into a timestamp.
    $timestamp = strtotime($ymd);

    //Convert it to DD-MM-YYYY
    $dmy = date("m/d/Y", $timestamp);
    $json2[] = array(
        'id' => $row["id"],
        'images' => $row["images"],
        'note' => $row["note"],
        'date' => $dmy,

    );
}
$json['user_images'] = $json2;
echo json_encode(array($json));
$conn->close();

This data only has 5 columns of data, but when I limit it to 1 the react native side fetches the data quickly. 该数据只有5列数据,但是当我将其限制为1时,react native会快速获取数据。 Is there a way I can speed up the fetch while still having all of my data results? 有没有一种方法可以在我仍然拥有所有数据结果的同时加快获取速度?

  1. You should have unuque index on username column on users table. 您应该在用户表的用户名列上具有唯一索引。 This will also get ride of your assumption that only one row will return. 您也可以假设只有一行会返回。 And it will also be faster because MySQL will return when finding one record. 而且它也会更快,因为MySQL在找到一条记录时会返回。
  2. (I didn't notice OP is on MyISAM.) It seems that your table is denomorlized and you are using username to query. (我没注意到OP在MyISAM上。)看来您的表已被删除,并且您正在使用用户名进行查询。 What if username change? 如果更改用户名怎么办? Will you update user_images table? 您将更新user_images表吗?
  3. Your user_images table should have an auto_increment primary key, so that your order by can be order by id desc, instead of date. 您的user_images表应具有一个auto_increment主键,以便您的order by可以按id desc而不是日期进行排序。 You can have one index on username, but this also depends on how selective of your where statement. 您可以在用户名上有一个索引,但这还取决于您对where语句的选择性。 If username has a very low cardinality, a full table scan will be chosen by MySQL. 如果用户名的基数非常低,则MySQL将选择全表扫描。
  4. You should also try to get the data in one query (join), instead of two. 您还应该尝试通过一个查询(联接)而不是两个查询来获取数据。
  5. You should use prepared statement for protecting you against SQL injection. 您应该使用准备好的语句来防止SQL注入。 Your code is vulnerable now. 您的代码现在容易受到攻击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM